Skip to content

Commit 76502d1

Browse files
authored
docs: Added documentation for new on conflict do nothing functionality for DB inserts (#435)
1 parent 46c6999 commit 76502d1

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

docs/06-concepts/06-database/05-crud.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,23 @@ var companies = await Company.db.insert(session, rows);
4343
In previous versions of Serverpod the `insert` method mutated the input object by setting the `id` field. In the example above the input variable remains unmodified after the `insert`/`insertRow` call.
4444
:::
4545

46+
### Ignoring conflicts
47+
48+
When inserting rows that might violate a unique or exclusion constraint, you can set `ignoreConflicts` to `true` on the `insert` method. Rows that would cause a unique constraint violation are silently skipped, and only the non-conflicting rows are inserted.
49+
50+
```dart
51+
var rows = [Company(name: 'Serverpod'), Company(name: 'Google')];
52+
var inserted = await Company.db.insert(session, rows, ignoreConflicts: true);
53+
```
54+
55+
The method returns only the rows that were successfully inserted. If all rows conflict, an empty list is returned.
56+
57+
This is useful for idempotent operations where you want to insert data without failing on duplicates.
58+
59+
:::note
60+
Under the hood, this uses PostgreSQL's `ON CONFLICT DO NOTHING`. Only unique and exclusion constraint violations are ignored — other errors such as `NOT NULL`, `CHECK`, or foreign key violations will still throw an exception.
61+
:::
62+
4663
## Read
4764

4865
There are three different read operations available.

0 commit comments

Comments
 (0)