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

Deprecate a ManyToMany field tested with TransactionTestCase #22

Open
annkamsk opened this issue Aug 16, 2022 · 0 comments
Open

Deprecate a ManyToMany field tested with TransactionTestCase #22

annkamsk opened this issue Aug 16, 2022 · 0 comments

Comments

@annkamsk
Copy link

In short: using deprecate_field on ManyToMany field and then creating the model inside TransactionTestCase raises ERROR: cannot truncate a table referenced in a foreign key constraint (at least in Postgres).

Explanation:
Django's TransactionTestCase uses truncate to flush the DB after each test. truncate is called on all existing tables simultaneously to avoid issues with any of them having an FK to another (in Postgres: “truncate cannot be used on a table that has FK references from other tables, unless all such tables are also truncated in the same command”). Django searches for table names by accessing Model._meta.db_table and Model._meta.local_many_to_many . But since deprecate_field makes the field getter return None, truncate isn’t called on the table that stores many-to-many relationship. Consequently, a DB error is raised.

This sounds like a pretty niche case and not sure if it's even worth fixing, since quite a lot of stars have to align for you to experience this. Nonetheless, I decided to share to spare others the pain of debugging.

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

1 participant