Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect index creation order #151

Closed
Chise1 opened this issue May 8, 2021 · 2 comments
Closed

Incorrect index creation order #151

Chise1 opened this issue May 8, 2021 · 2 comments

Comments

@Chise1
Copy link

Chise1 commented May 8, 2021

Now I have a model like this:

class Author(Model):
    name = fields.CharField(max_length=255)

I update it to this:

class Author(Model):
    name = fields.CharField(max_length=255)
    nickname = fields.CharField(max_length=32)
    class Meta:
        unique_together=(("name","nickname"),)

The sql file has an error: It will create unique index before create field.

@boh5
Copy link

boh5 commented May 31, 2021

I may have encountered the same problem.
I have a model like this:

class User(BaseModel):
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=128, unique=True, description='名称')
    password_hash = fields.CharField(max_length=128, description='密码哈希')

    metas: fields.ReverseRelation['Meta']

class Meta(BaseModel):
    id = fields.IntField(pk=True)
    clip_title = fields.CharField(max_length=512, description='母本标题')
    user: fields.ForeignKeyRelation['User'] = fields.ForeignKeyField('models.User', related_name='metas')

I have migrated and uupgraded this model.
Now, I want to add a field and add a unique key with this field:

class Meta(BaseModel):
    id = fields.IntField(pk=True)
    custom_id = fields.CharField(max_length=128, description='自定义ID')  # add a field
    clip_title = fields.CharField(max_length=512, description='母本标题')
    user: fields.ForeignKeyRelation['User'] = fields.ForeignKeyField('models.User', related_name='metas')

    class Meta:
        unique_together = ('custom_id', 'user')   # add unique key with new field

The SQL generated by aerich is:

-- upgrade --
ALTER TABLE `meta` ADD `custom_id` VARCHAR(128) NOT NULL  COMMENT '自定义ID';
ALTER TABLE `meta` ADD UNIQUE INDEX `uid_meta_custom__2e5d62` (`custom_id`, `user_id`);
-- downgrade --
ALTER TABLE `meta` DROP INDEX `uid_meta_custom__2e5d62`;
ALTER TABLE `meta` DROP COLUMN `custom_id`;

The order of sql is not correct, so upgrade cannot be applied.

@long2ice
Copy link
Member

long2ice commented Jun 1, 2021

Fixed

@long2ice long2ice closed this as completed Jun 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants