Skip to content

Repeated generation migration #2176

@panla

Description

@panla

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions