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

Problems with migration of foreign key contraints #262

Open
vlakius opened this issue Sep 23, 2022 · 4 comments
Open

Problems with migration of foreign key contraints #262

vlakius opened this issue Sep 23, 2022 · 4 comments
Labels
enhancement New feature or request

Comments

@vlakius
Copy link

vlakius commented Sep 23, 2022

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
Copy link

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

@vlakius
Copy link
Author

vlakius commented Sep 23, 2022

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
Copy link

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.

@waketzheng waketzheng added the enhancement New feature or request label Dec 22, 2024
@waketzheng
Copy link
Contributor

Relate #149

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants