tortoise-orm
tortoise-orm copied to clipboard
How do ON DUPLICATE KEY UPDATE?
Table have a unique constrain on 2 fields.
When i try to await MyModel.create(**dict_of_data)
with exist entry in unique fields, i got error:
tortoise.exceptions.IntegrityError: (1062, "Duplicate entry...
Try await MyModel(**dict_of_data).save(force_update=True)
raise error:
tortoise.exceptions.IntegrityError: Can't update object that doesn't exist. PK: None
How can i do ON DUPLICATE KEY UPDATE
for create new or update exist record without many requests and conditional structures?
Try update_or_create
or bulk_create
Try
update_or_create
orbulk_create
update_or_create
working not correct:
tortoise/models.py
[1090] instance = await cls.select_for_update().using_db(connection).get_or_none(**kwargs)
wrong return None instance and then later "Duplicate entry" exception init if any field in DB not equal new data. Does it matching all parameters, or only unique fields? I guess - all them, but why? Logically need only unique. I dont understand how it use. Why is there so much bureaucracy for such simple operation supported by database?
Can we check by primary key's existence and then determine whether to create or update ?
Would check by primary key be better than check by the whole row data's identity for determine whether to create or update ?