You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/manager.md
+43Lines changed: 43 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -170,6 +170,49 @@ This is also known as a "upsert".
170
170
171
171
This is preferable when the data you're about to insert is the same as the one that already exists. This is more performant because it avoids a write in case the row already exists.
172
172
173
+
### Bulk
174
+
`bulk_insert` allows your to use conflict resolution for bulk inserts:
175
+
176
+
from django.db import models
177
+
from psqlextra.models import PostgresModel
178
+
179
+
class MyModel(PostgresModel):
180
+
name = models.CharField(max_length=255, unique=True)
181
+
182
+
obj = (
183
+
MyModel.objects
184
+
.on_conflict(['name'], ConflictAction.UPDATE)
185
+
.bulk_insert([
186
+
dict(name='swen'),
187
+
dict(name='henk'),
188
+
dict(name='adela')
189
+
])
190
+
)
191
+
192
+
`bulk_insert` uses a single query to insert all specified rows at once.
193
+
194
+
#### Limitations
195
+
In order to stick to the "everything in one query" principle, various, more advanced usages of `bulk_insert` are impossible. It is not possible to have different rows specify different amounts of columns. The following example does **not work**:
dict(name='henk', last_name='poepjes'), # invalid, different column configuration
210
+
dict(name='adela')
211
+
])
212
+
)
213
+
214
+
An exception is thrown if `django-postgres-extra` detects this behavior.
215
+
173
216
### Shorthand
174
217
The `on_conflict`, `insert` and `insert_or_create` methods were only added in `django-postgres-extra` 1.6. Before that, only `ConflictAction.UPDATE` was supported in the following form:
0 commit comments