insertAll method

Future<void> insertAll(
  1. Iterable<Insertable<Row>> rows, {
  2. InsertMode? mode,
  3. UpsertClause<Tbl, Row>? onConflict,
})

Atomically inserts all rows into the table.

The mode and onConflict clause can optionally control the behavior of an insert if an existing row conflicts with a new one. Unlike calling Batch.insertAll in a Batch directly, foreign keys are checked only after all inserts ran. In other words, the order in which the rows are in doesn't matter if there are foreign keys between them.

Implementation

Future<void> insertAll(Iterable<Insertable<Row>> rows,
    {InsertMode? mode, UpsertClause<Tbl, Row>? onConflict}) {
  return attachedDatabase.batch((b) {
    // TODO: Remove this pragma, it should be an explicit opt-in.
    // https://github.com/simolus3/drift/issues/3393
    if (attachedDatabase.executor.dialect == SqlDialect.sqlite) {
      // Note that this batch runs in a transaction, the pragma will be reset
      // when the transaction completes.
      b.customStatement('pragma defer_foreign_keys = on;');
    }

    b.insertAll(this, rows, mode: mode, onConflict: onConflict);
  });
}