Moor: Filter duplicate Insertion

Created on 3 Nov 2020  路  10Comments  路  Source: simolus3/moor

Is there moor way to automatically filter duplicate data insertion , especially at moor files ?

question

Most helpful comment

if you also insert the primary key (id), you can do that on the table level:

CREATE TABLE user (
  id INTEGER,
  name TEXT,
  email TEXT,
  primary key ("id") on conflict replace
);

All 10 comments

You could add a unique constraint and then use INSERT OR IGNORE INTO ... to avoid inserting data twice.

Companion is cleaner way. Can I get like this INSERT OR IGNORE INTO user VALUES :userCompanion ?

I mean you can write into(user).insert(userCompanion, mode: InsertMode.insertOrIgnore) in Dart. I can take a look at supporting companions inside moor files too, but it'll probably be some work.

This is clean too. Thx for ur help. This issue can be closed now

Wait! sadly this does not filter duplication here. How insertOrIgnore works?

How can I detect the duplication by a column ? Example, detect email column of incoming data for filtering.

How can I detect the duplication by a column

If you want that column to be unique, you can add a unique column constraint. That should work together with INSERT OR IGNORE.

Or do you want to allow duplicate values in general, but avoid them when inserting data in a batch?

Cool if I can define table like this in moor file and InsertMode.insertOrIgnore automatically detect it

CREATE TABLE user (
  id INTEGER PRIMARY KEY NOT NULL,
  name TEXT,
  email TEXT,
  UNIQUE(id,email)
);

The UNIQUE(id, email) key won't do much - id is the sole primary key, so it's unique on its own. You can just use a UNIQUE (email) constraint if you want them to be unique.

if you also insert the primary key (id), you can do that on the table level:

CREATE TABLE user (
  id INTEGER,
  name TEXT,
  email TEXT,
  primary key ("id") on conflict replace
);
Was this page helpful?
0 / 5 - 0 ratings