hhtokpinar / sqfEntity

SqfEntity ORM for Flutter/Dart lets you build and execute SQL commands on SQLite database easily and quickly with the help of fluent methods similar to .Net Entity Framework. SqfEntity also generates add/edit forms with validations and special controls (DropDown List, DateTime pickers, Checkboxes.. etc) for your table.
379 stars 100 forks source link

How to remove the duplicates? #160

Closed Niroop4444 closed 3 years ago

Niroop4444 commented 3 years ago

I'm trying to remove the duplicate values from the database by using the delete method where-in I'm comparing the values of the ones in the database and the ones which are being inserted but both the original and the duplicate is getting deleted. I did try the upsertAll method also but getting an error as type 'Future' used in the 'for' loop must implement iterable.

hhtokpinar commented 3 years ago

Hi @Niroop4444 Have you solved it yet? You can try to select a grouped list on the table without filtering using that groupBy(...all fields of the table...) method and add them to a new table

NiroopNife commented 3 years ago

Hello @hhtokpinar, what i did was, i made the items in the list to be seleted individually through the FOR loop, then i just found if any duplicate is present of not by using

var dup = await Article().select().articleNumber.contains(articles[i]).toSingle(); //Finding the duplicates
   if (dup != null) {
       print(articleNumber + "is already present");
       return null;
   } else {
       print('add to db');
   }

Was my approach a right way? Or is there any much simpler way of acheiving it?

hhtokpinar commented 3 years ago

Why don’t you set the articleNumber field as a primaryKey? So it doesn’t allow duplicate articles. I guess It’s the best way for you

NiroopNife commented 3 years ago

Yeah thats the best way, but the parameters for that field are just PrimaryKeyType.integer_auto_incremental, PrimaryKeyType.integer_unique & PrimaryKeyType.text. So how to get make the articleNumber field as primaryKey?

hhtokpinar commented 3 years ago

Could you send a sample row as a json format?

NiroopNife commented 3 years ago

Yes certainly, this is how I'm saving the details to DB

const tableArticles = SqfEntityTable(
    tableName: 'articles',
    primaryKeyName: 'articleId',
    primaryKeyType: PrimaryKeyType.integer_auto_incremental,
    useSoftDeleting: true,
    fields: [
      SqfEntityField('articleNumber', DbType.text),
      SqfEntityField('remarkTime', DbType.text),
      SqfEntityField('currentLocation', DbType.text),
      SqfEntityField('deliveryStatus', DbType.text),
      SqfEntityField('addresseeType', DbType.text),
      SqfEntityField('deliveredTo', DbType.text),
      SqfEntityField('notDeliveredRemark', DbType.text),
    ]);

& this is how its getting saved

[{articleId: 1, articleNumber: RK591158945IN, remarkTime: 05-02-2021 12:54:11, currentLocation: 12.3069  76.6726, deliveryStatus: Delivered, addresseeType: Addressee, deliveredTo: hjghbhb, notDeliveredRemark: , isDeleted: 0}]
hhtokpinar commented 3 years ago

Ok. you should define it like this:

const tableArticles = SqfEntityTable(
    tableName: 'articles',
    primaryKeyName: 'articleNumber',
    primaryKeyType: PrimaryKeyType.text,
    useSoftDeleting: true,
    fields: [
      SqfEntityField('remarkTime', DbType.text),
      SqfEntityField('currentLocation', DbType.text),
      SqfEntityField('deliveryStatus', DbType.text),
      SqfEntityField('addresseeType', DbType.text),
      SqfEntityField('deliveredTo', DbType.text),
      SqfEntityField('notDeliveredRemark', DbType.text),
    ]);

inserting a new record:

final article = Articles();
article.articleNumber: 'RK591158945IN';
//  set the other values
await article.save();

and check saveResult if it's already exist

if(!article.saveResult.success)
{
// return some error message
}

to make changes on existing record:

final article = await Articles().getById('RK591158945IN');
// TO DO make some changes
article.save();
NiroopNife commented 3 years ago

This was too helpful, thank you very much

hhtokpinar commented 3 years ago

You're welcome...

hhtokpinar commented 3 years ago

or you can also try this way:

final article = await Articles().getById('RK591158945IN');
// create a new one if not exist
if(article == null) 
{ 
  article = Articles();
  article.articleNumber: 'RK591158945IN';}
}
  // set values
article.save();