insertOnConflictUpdate method
Attempts to insert entity
into the database. If the insert would
violate a primary key or uniqueness constraint, updates the columns that
are present on entity
.
Note that this is subtly different from InsertMode.replace! When using
InsertMode.replace, the old row will be deleted and replaced with the
new row. With insertOnConflictUpdate, columns from the old row that are
not present on entity
are unchanged, and no row will be deleted.
Be aware that insertOnConflictUpdate uses an upsert clause, which is not available on older sqlite implementations. Note: By default, only the primary key is used for detect uniqueness violations. If you have further uniqueness constraints, please use the general insert method with a DoUpdate including those columns in its DoUpdate.target.
Liek the other insert methods, this returns the rowid of the last
insert. When the insert was turned into an update due to a conflicting
row, this behavior is somewhat confusing because the id of an (likely
unrelated) insert is returned instead.
Use insertReturning with an onConflict
clause of
DoUpdate((_) => entity)
to reliably get the written entity instead.
Implementation
Future<int> insertOnConflictUpdate(Insertable<D> entity) {
return insert(entity, onConflict: DoUpdate((_) => entity));
}