tortoise / tortoise/tortoise-orm

How do ON DUPLICATE KEY UPDATE?

Open
#1,066 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.6k
Forks
516
Avg merge
2d 21h
Merged PRs (30d)
9

Description

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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No implementation file or test is named. Start by locating the ORM’s create and save APIs and checking backend support for MySQL’s ON DUPLICATE KEY UPDATE; done means a documented single-operation create-or-update path for a two-column unique constraint, with behavior verified by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
mysql, python
Domain
backend, databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.