Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to see SQL query that caused error in Django migration?

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.

like image 330
Ariel Avatar asked Dec 13 '18 09:12

Ariel


People also ask

How can I see SQL query generated by Django?

Documentation and source is available at http://django-debug-toolbar.readthedocs.io/. debug_toolbar is especially useful when you have a query that's failing with a SQL syntax error; it will display the last query that attempted to run (and failed), making it easier to debug.

How do I fix migration issues in Django?

you can either: temporarily remove your migration, execute python manage.py migrate, add again your migration and re-execute python manage.py migrate. Use this case if the migrations refer to different models and you want different migration files for each one of them.

How do I see previous migration in Django?

You can revert by migrating to the previous migration. For example, if your last two migrations are: 0010_previous_migration. 0011_migration_to_revert.

How does Django keep track of migration?

Django keeps track of applied migrations in the Django migrations table. Django migrations consist of plain Python files containing a Migration class. Django knows which changes to perform from the operations list in the Migration classes. Django compares your models to a project state it builds from the migrations.

What command do we use in Django to see raw SQL of the migration?

Use the sqlmigrate command from manage.py . will display the SQL statements for a specific migration of the app.


1 Answers

You can print SQL statements for particular migration by using following management command.

python manage.py sqlmigrate <app label> <migration_name to print sql for>

For an instance if you want to check migration for hello app in your django project and suppose you want to print sql statements for 0001_initial.py. Than you need to execute following line.

python manage.py sqlmigrate hello 0001_initial.py

It would print SQL definations (statements) that will be executed on migration.

like image 52
Devang Padhiyar Avatar answered Oct 11 '22 04:10

Devang Padhiyar