I'm trying to run a data migration that deletes all rows in a table (say, MyModel
). There is another table that points to that table (RelatedModel
). The field in the RelatedModel
that maps to MyModel
has on_delete=models.SET_NULL
. When I run the migration, however, I get:
File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 211, in _commit
return self.connection.commit()
IntegrityError: update or delete on table "MyModel" violates foreign key constraint "f2274d6f2be82bbff458f3e5487b1864" on table "RelatedModel"
DETAIL: Key (uuid)=(ywKMUYx7G2RoK9vqqEWZPV) is still referenced from table "RelatedModel".
I added a breakpoint in the migration and checked the SQL DELETE queries. I ran them interactively in the shell and they worked inside the transaction, but the migration still breaks when it tries to commit it. I can't see however which query exactly causes this error, so I don't know how to debug this. Any suggestions? Thanks.
PS: I'm using Django 1.9.13, Python 2.7, PostgreSQL 10.4.