tortoise / aerich

A database migrations tool for TortoiseORM, ready to production.
https://github.com/tortoise/aerich
Apache License 2.0
804 stars 90 forks source link

Problems with migration of foreign key contraints #262

Open vlakius opened 1 year ago

vlakius commented 1 year ago

I have already initialized the database with aerich init-db etc.. and the migration works correctly except when I try to change foreign key properties my models (simplified)

class Intervents(Model):
    """ Tabella Interventi """
    id = fields.IntField(pk=True)
    creation_date = fields.DatetimeField(auto_now_add=True)

    # Foreign Key
    client = fields.ForeignKeyField('models.Clients', related_name='intervents', null=True, on_delete=fields.CASCADE)

class Clients(Model):
    """ Tabella Clienti """
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=60, unique=True, index=True)

I try to change the constraints to on_delete=fields.RESTRINCT

But when I try to do aerich migrate it tells me no changes detected As ORM I am using Tortoise ORM and the database is MYSQL Am I missing something ?

alexf-bond commented 1 year ago

Did you run aerich upgrade prior to making the on_delete code change and then run aerich migrate?

vlakius commented 1 year ago

Did you run aerich upgrade prior to making the on_delete code change and then run aerich migrate?

no, but I just tried and it keeps giving me "no changes detected". also if I try to delete null=True and change the field on_delete=fields.CASCADE doing aerich migrate the change is detected but the _update.sql file shows no operation on contraints

-- upgrade --
ALTER TABLE `intervents` MODIFY COLUMN `client_id` INT NOT NULL ';
-- downgrade --
ALTER TABLE `intervents` MODIFY COLUMN `client_id` INT
alexf-bond commented 1 year ago

Fairly certain the fields.RESTRICT is a code level concept (similar to default= field arg in sqlalchemy) and at the database level it's just a non-nullable column that has a foreign key which would be managed by mysql itself.