Insert data into database
Following methods allow you to insert data into the database.
create(on_conflict=None, conflict_target=None, update_fields=None, update_values=None, **kwargs) -> Optional[Model]get_or_create(_defaults: Optional[dict[str, Any]] = None, **kwargs) -> tuple[Model, bool]update_or_create(**kwargs) -> Model-
bulk_create(objects: list[Model], on_conflict=None, conflict_target=None, update_fields=None, update_values=None) -> None -
ModelModel.save()methodModel.upsert()methodModel.save_related()method
-
QuerysetProxyQuerysetProxy.create(**kwargs)methodQuerysetProxy.get_or_create(_defaults: Optional[dict[str, Any]] = None, **kwargs)methodQuerysetProxy.update_or_create(**kwargs)method
create
create(**kwargs): -> Model
Creates the model instance, saves it in a database and returns the updates model (with pk populated if not passed and autoincrement is set).
The allowed kwargs are Model fields names and proper value types.
| Python | |
|---|---|
1 2 3 4 5 6 7 8 9 10 | |
| Python | |
|---|---|
1 2 | |
The alternative is a split creation and persistence of the Model.
| Python | |
|---|---|
1 2 | |
Tip
Check other Model methods in models
get_or_create
get_or_create(_defaults: Optional[dict[str, Any]] = None, **kwargs) -> tuple[Model, bool]
Combination of create and get methods.
Tries to get a row meeting the criteria and if NoMatch exception is raised it creates
a new one with given kwargs and _defaults.
When _defaults dictionary is provided the values set in _defaults will always be set, including overwriting explicitly provided values.
i.e. get_or_create(_defaults: {"title": "I win"}, title="never used") will always use "I win" as title whether you provide your own value in kwargs or not.
| Python | |
|---|---|
1 2 3 4 5 6 7 8 9 10 | |
| Python | |
|---|---|
1 2 3 4 5 6 7 8 9 | |
Warning
Despite being a equivalent row from database the album and album2 in
example above are 2 different python objects!
Updating one of them will not refresh the second one until you explicitly load() the
fresh data from db.
Note
Note that if you want to create a new object you either have to pass pk column value or pk column has to be set as autoincrement
If a concurrent caller inserts the matching row first, the failed insert is caught and the row is fetched again. Inside database.transaction() the insert runs in a savepoint, so a failed insert doesn't abort the transaction.
Warning
On MySQL with the default REPEATABLE READ isolation, the retried lookup inside a transaction reads the snapshot taken at the transaction's first read, so it can't see the row a concurrent transaction committed later and the IntegrityError is raised instead.
update_or_create
update_or_create(**kwargs) -> Model
Updates the model, or in case there is no match in database creates a new one.
| Python | |
|---|---|
1 | |
Note
Note that if you want to create a new object you either have to pass pk column value or pk column has to be set as autoincrement
Tip
update_or_create() looks the row up and then updates or inserts it in a separate
statement, so concurrent calls can raise IntegrityError or overwrite each other.
For an atomic insert or update by a unique key use
create(on_conflict="update").
bulk_create
bulk_create(objects: list["Model"]) -> None
Allows you to create multiple objects at once.
A valid list of Model objects needs to be passed.
| Python | |
|---|---|
1 | |
Note
Objects are inserted with one INSERT per distinct set of provided fields.
If some objects set a value that others leave out (e.g. an explicit primary key
or a field with server_default), they are inserted in separate statements
inside one transaction, so generated primary keys may not follow the order
of the passed list. If you need that order, split such objects into separate
bulk_create() calls.
Conflict handling
create() and bulk_create() can skip or update rows that conflict with existing
ones on a unique key, in the same statement as the insert, instead of failing with an
IntegrityError.
| Python | |
|---|---|
1 2 3 4 5 6 7 | |
Skip conflicting rows
| Python | |
|---|---|
1 2 3 | |
on_conflict="ignore" reacts to a conflict on any unique key (primary key, unique
column or unique constraint). Concurrent create(on_conflict="ignore") calls insert the
row once, the others get None, and no error is raised, so an open transaction stays
usable. pre_save is sent, post_save only when the row was inserted.
None does not load the existing row: use get() for it. The model built from the
passed kwargs is still added to the in-memory relations of models it was given, like
after a failed insert.
| Python | |
|---|---|
1 2 3 | |
Update conflicting rows
| Python | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
conflict_target- fields of the unique key the rows conflict on: the primary key by default. A foreign key stored in several columns stands for all of its columns.update_fields- fields set from the inserted row: by default all inserted fields except the target and the primary key, none whenupdate_valuesare given.update_values- values or column expressions set on conflict, see below.- Fields with
on_updatethat are in neitherupdate_fieldsnorupdate_valuesget theiron_updatevalue, as inbulk_update().
conflict_target, update_fields and update_values are only allowed with
on_conflict="update".
When several objects of one call conflict on the same key, the last one wins.
create(on_conflict="update") inserts or updates the row in one statement and returns
the stored row, so fields left out of update_fields hold the values of the existing
row. Every field of the target needs a value (an autoincrement primary key that is not
passed cannot conflict). It sends pre_save and post_save, whether the row was
inserted or updated.
bulk_create() sends no signals, sets no primary keys and marks all passed objects as
saved, the skipped ones too.
Expressions on conflict
update_values sets fields of the conflicting row to a value or to a
column expression computed by the database, in the same
statement as the insert, so concurrent calls never lose an update. In it F("field")
(or Model.field) is the column of the existing row and ormar.Inserted("field")
the value the insert tried to write:
| Python | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | |
| PostgreSQL, SQLite | MySQL | |
|---|---|---|
F("x") |
table.x (the existing row) |
table.x (the existing row) |
Inserted("x") |
excluded.x |
VALUES(x) |
- When
update_valuesare given,update_fieldsdefault to none: otherwise the insertedcompleted=1would be stored instead of the incremented value. List fields that should take the inserted value inupdate_fieldsnext toupdate_values; a field in both is rejected. - The rules of expressions in
update()apply: an expression reads non-relation fields of the inserted model only (an upsert joins no tables, so no relation paths), sets no relation and reads no field set by the same upsert (update_fields, otherupdate_values, fields withon_update); reading the field it sets is fine, andInserted()can read any field. The literal goes on the right side. Plain values ("source": "merge") are validated likeupdate()values. Inserted()is only accepted inupdate_values; in filters andupdate()it raisesQueryDefinitionError, as do unknown fields.- The statement is cached per shape of the expressions:
F("completed") + 1andF("completed") + 5run the same SQL with a different bound value. - On MySQL a multi-row
bulk_create()stores the literals ofupdate_valuesin session user variables (@ormar_u<n>_<k>) together with theon_updatevalues, see the warning below.
Backends
| PostgreSQL, SQLite | MySQL | |
|---|---|---|
create(on_conflict="ignore") |
INSERT ... ON CONFLICT DO NOTHING RETURNING |
plain INSERT, a duplicate key error is caught |
create(on_conflict="update") |
INSERT ... ON CONFLICT (target) DO UPDATE ... RETURNING |
ON DUPLICATE KEY UPDATE, then a select by the target values |
bulk_create(on_conflict="ignore") |
ON CONFLICT DO NOTHING |
ON DUPLICATE KEY UPDATE of the primary key to itself |
bulk_create(on_conflict="update") |
ON CONFLICT (target) DO UPDATE |
ON DUPLICATE KEY UPDATE; on_update values and update_values literals are set once in user variables (SET @ormar_on_update_<n>, @ormar_u<n>) and read by all rows |
Warning
MySQL has no conflict target: ON DUPLICATE KEY UPDATE reacts to a conflict on
any unique key of the table, so with several unique keys a row can update an
existing row that conflicts on a different key than conflict_target. The target is
used only on PostgreSQL and SQLite, which reject it when it does not match a unique
key or constraint. create(on_conflict="update") reads the row back by the target
values and raises NoMatch when none matches: the conflict was on another key (that
row is updated already), or the database stored a target value differently (floats,
datetimes with fractional seconds). MariaDB is not tested.
Warning
On MySQL a multi-row bulk_create(on_conflict="update") of a model with on_update
fields or with literals in update_values stores those values in session user
variables (@ormar_on_update_<n>, @ormar_u<n>) right before the insert. Do not run such calls concurrently
on one connection (e.g. with asyncio.gather() inside one transaction()): one
call can overwrite the values of the other. Separate connections are not affected.
Note
SQLite supports ON CONFLICT with RETURNING from version 3.35.
Note
on_conflict, conflict_target, update_fields and update_values are reserved
keyword arguments of create(). To create a model with a field of such a name use
Model(on_conflict=...).save().
QuerysetProxy.create() accepts on_conflict too, except for many to many
relations. Through a reverse foreign key the relation is always updated, so a
conflicting row of another owner moves to the one it was created through.
create(on_conflict="update") with nothing to update (only the target fields passed)
works as an atomic get or create by a unique key: the conflicting row is returned
unchanged.
Model methods
Each model instance have a set of methods to save, update or load itself.
save
You can create new models by using QuerySet.create() method or by initializing your model as a normal pydantic model
and later calling save() method.
Tip
Read more about save() method in models-save
upsert
It's a proxy to either save() or update(**kwargs) methods of a Model.
If the pk is not set the save() method will be called.
Tip
Read more about upsert() method in models-upsert
save_related
Method goes through all relations of the Model on which the method is called,
and calls upsert() method on each model that is not saved.
Tip
Read more about save_related() method in models-save-related
QuerysetProxy methods
When access directly the related ManyToMany field as well as ReverseForeignKey returns the list of related models.
But at the same time it exposes subset of QuerySet API, so you can filter, create, select related etc related models directly from parent model.
create
Works exactly the same as create function above but allows you to create related objects from other side of the relation.
Tip
To read more about QuerysetProxy visit querysetproxy section
get_or_create
Works exactly the same as get_or_create function above but allows you to query or create related objects from other side of the relation.
Tip
To read more about QuerysetProxy visit querysetproxy section
update_or_create
Works exactly the same as update_or_create function above but allows you to update or create related objects from other side of the relation.
Tip
To read more about QuerysetProxy visit querysetproxy section