aerich icon indicating copy to clipboard operation
aerich copied to clipboard

Problems with migration of foreign key contraints

Open vlakius opened this issue 3 years ago • 3 comments
trafficstars

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 ?

vlakius avatar Sep 23 '22 09:09 vlakius

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

alexf-bond avatar Sep 23 '22 15:09 alexf-bond

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

vlakius avatar Sep 23 '22 16:09 vlakius

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.

alexf-bond avatar Sep 23 '22 16:09 alexf-bond