tortoise / tortoise/tortoise-orm

v0.19: bulk_create doesn't work correctly with on_conflict

Open
#1,422 2 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

Describe the bug
bulk_create works not correctly with on_conflicts.
raise tortoise.exceptions.OperationalError: (1064, "You have an error in your SQL syntax")

To Reproduce

The SQL syntax seems to be error on Mysql-5.7.28

cls.MODEL.bulk_create(results, update_fields=['update_time'], on_conflict=['domain_name']).sql()

INSERT INTO `domain` (`create_time`,`update_time`,`domain_name`,`describe`,`data_type`,`rule`,`is_standard`,`is_union`,`relate_domain`,`encrypt_key`) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) AS `new_domain` ON DUPLICATE KEY UPDATE `update_time`=`new_domain`.`update_time`;

Exception

check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS `new_domain` ON DUPLICATE KEY UPDATE `update_time`=`new_domain' at line 1

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

Start at the bulk_create SQL generation path for on_conflict and reproduce the reported .sql() output against MySQL 5.7. Compare the generated statement with MySQL 5.7 syntax and verify that bulk_create with update_fields and on_conflict executes successfully without the reported syntax error.

Written by the indexing model from the issue text.

Assessment

Tech stack
mysql, python
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.