insertOnConflictDoNothing method

SqlWrite insertOnConflictDoNothing(
  1. SqlTable<Object?> table,
  2. Iterable<SqlAssignment> values, {
  3. required Iterable<SqlTableColumn<Object?>> target,
})

Creates a single-row INSERT that does nothing on a matching conflict.

values and target must both be non-empty. Every target column must belong to table. The column list must match a conflict arbiter accepted by the selected database; use BoundSql for other index shapes.

SQLite, Cloudflare D1, and PostgreSQL use conflict-target ON CONFLICT DO NOTHING. MySQL is rejected before database I/O because it has no equivalent conflict-target operation.

Implementation

SqlWrite insertOnConflictDoNothing(
  SqlTable<Object?> table,
  Iterable<SqlAssignment> values, {
  required Iterable<SqlTableColumn<Object?>> target,
}) {
  if (!supportsSqlOnConflictDoNothing(dialect)) {
    throw const SqlException(
      SqlErrorCode.unsupported,
      'MySQL does not support conflict-target ON CONFLICT DO NOTHING.',
    );
  }
  final assignments = _validateAssignments(table, values);
  if (assignments.isEmpty) {
    throw ArgumentError(
      'A conflict-target SQL INSERT requires at least one assignment.',
    );
  }
  final columns = List<SqlTableColumn<Object?>>.unmodifiable(target);
  if (columns.isEmpty) {
    throw ArgumentError(
      'A SQL conflict target requires at least one column.',
    );
  }
  final names = <String>{};
  for (final column in columns) {
    _requireColumn(table, column);
    if (!names.add(column.name)) {
      throw ArgumentError(
        'Conflict target column "${column.name}" is used more than once.',
      );
    }
  }
  return _insert(table, assignments, conflictTarget: columns);
}