tortoise / tortoise/tortoise-orm

Ensure generate_schema EXACTLY the same as SHOW CREATE TABLE in MySQL

Open
#237 14 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Describe the bug
As you may know, I'm working on a schema sync to MySQL which should work with Tortoise ORM. For this to work good the schema that Tortoise generate should be as close to the result of SHOW CREATE TABLE as possible. Now it works quite well, though I still have a problem with unique_together.

To Reproduce
See the example in #215 .
The generated SQL is semantically good, though MySQL will give a name to the unique key, which is the name of the 1st field it uses. But if you have another key with that name, it will add a number to it. So it is not deterministic enough.

Expected behavior
It should look like this:

UNIQUE KEY `key_user_id_62ed175d` (`key`, `user_id`)

Additional context
Also the full command has the KEY keyword, tortoise generates code without it, though I could resolve it with regexp in my schema sync.

I still need a similar custom schema generator described in #215 to solve this.

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 with the generate_schema entry point and compare its unique_together output with the SHOW CREATE TABLE example from #215. Check how MySQL names unique keys and whether the generated statement includes KEY. Done means the output uses the expected deterministic key name and matches the relevant SHOW CREATE TABLE syntax.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.