﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
23091	Squashed migrations may try to add existing constraint if there was RunPython between create and alter some-model migrations	anonymous	Andrew Godwin	"I squashed a lot of migrations, some with RunPython functions. The squashed migration needed some fixing to work. At some point of the squashed migration there was a ""CreateModel"" operation that created some model with relation to User:


{{{
('user', models.ForeignKey(blank=True, to_field='id', to=settings.AUTH_USER_MODEL, null=True)),
}}}

But as the models got edited there was a migration (rather pointless, caused by editable=False):


{{{
field=models.ForeignKey(blank=True, null=True, to_field='id', to=settings.AUTH_USER_MODEL, editable=False),
}}}

In between there was a RunPython which quite likely prevented squashing those two migrations into one. Running such migrations ended up with:

{{{
 File ""/var/www/buku/virtualenvs/1405079302/lib/python3.4/site-packages/django/db/backends/utils.py"", line 66, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: constraint ""buku_vendor_offerredir_user_id_75e4400f46095260_fk_auth_user_id"" for relation ""buku_vendor_offerredirect"" already exists

}}}

The SQL it was trying to run was:


{{{
ALTER TABLE ""buku_vendor_offerredirect"" ADD CONSTRAINT buku_vendor_offerredir_user_id_75e4400f46095260_fk_auth_user_id FOREIGN KEY (""user_id"") REFERENCES ""auth_user"" (""id"") DEFERRABLE INITIALLY DEFERRED []
}}}

It would be handy if that ""self.cursor.execute(sql, params)"" from django/db/backends/utils.py also printed the SQL it was trying to run so it would be easier to find the operation in migration (or make exception more verbose).

I had like 4 such cases. Some quite serious like removing null=True and adding unique=True"	Bug	closed	Migrations	1.7-rc-1	Release blocker	fixed			Accepted	0	0	0	0	0	0
