tortoise / tortoise/tortoise-orm

Repeated generation migration

Open
#2,176 9 comments 1 reaction 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

Repeated generation migration, remove id and then create id

To Reproduce

model

class Project(Model):
    id = fields.BigIntField(primary_key=True, source_field='id')

    name = fields.CharField(max_length=64, null=False, description='名称')
    desc = fields.CharField(max_length=1024, null=False, default='', description='描述')

    class Meta:
        app = 'default_db'
        table = 'projects'
        table_description = '项目表'

        indexes = (('name',),)
tortoise makemigrations

get 0001_initial.py

from tortoise import migrations
from tortoise.migrations import operations as ops
from orjson import loads
from tortoise.fields.base import OnDelete
from tortoise.fields.data import JSON_DUMPS
from tortoise import fields
from tortoise.indexes import Index

class Migration(migrations.Migration):
    initial = True

    operations = [
        
        ops.CreateModel(
            name='Project',
            fields=[
                ('name', fields.CharField(description='名称', max_length=64)),
                ('desc', fields.CharField(default='', description='描述', max_length=1024)),
            ],
            options={'table': 'projects', 'app': 'default_db', 'indexes': [Index(fields=['name'])], 'pk_attr': 'id', 'table_description': '项目表'},
            bases=['Model'],
        ),
        
    ]

run

tortoise migrate

create tables

no change model define code and run makemigrations

tortoise makemigrations

get 0002_auto_20260408_1716.py

from tortoise import migrations
from tortoise.migrations import operations as ops
from tortoise import fields
from tortoise.indexes import Index

class Migration(migrations.Migration):
    dependencies = [('default_db', '0001_initial')]

    initial = False

    operations = [
        ops.RemoveField(model_name='Project', name='id'),
        ops.AddField(
            model_name='Project',
            name='id',
            field=fields.BigIntField(source_field='id', generated=True, primary_key=True, unique=True, db_index=True),
        ),
        ops.AddIndex(
            model_name='Project',
            index=Index(fields=['id']),
        ),
    ]

remove pk, id and create pk, id and index

and.

if

id = fields.BigIntField(primary_key=True)

gen normal

ops.CreateModel(
            name='Project',
            fields=[
                ('id', fields.BigIntField(generated=True, primary_key=True, unique=True, db_index=True)),
                ('name', fields.CharField(description='名称', max_length=64)),
                ('desc', fields.CharField(default='', description='描述', max_length=1024)),
            ],
            options={'table': 'projects', 'app': 'default_db', 'indexes': [Index(fields=['name'])], 'pk_attr': 'id', 'table_description': '项目表'},
            bases=['Model'],
        ),

will normal

Expected behavior

gen normal migration

Additional context

tortoise-orm 1.1.7
mysql: 5.7

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 generated 0001_initial.py and 0002_auto_20260408_1716.py, comparing the model definition using source_field='id' with the definition without it. Reproduce the issue with tortoise makemigrations and verify completion when the unchanged model produces a normal migration instead of RemoveField, AddField, and AddIndex operations. The reported context is tortoise-orm 1.1.7 on MySQL 5.7.

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
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.