﻿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
25492	Check deferred foreign key constraints before dropping them	Joey Wilhelm	nobody	"Using Django 1.8.4 and PostgreSQL 9.4.4, I have run into a scenario where a migrations was created which generated an error when I attempted to apply it. The relevant operation from the migration is:

{{{#!python
        migrations.AlterField(
            model_name='charity',
            name='name_legal',
            field=models.CharField(max_length=255, default='', verbose_name='legal name'),
            preserve_default=False,
        ),
}}}

The actual change was to remove `null=True` and `blank=True` from the field.

When running the migration, I get the following error: 

{{{django.db.utils.OperationalError: cannot ALTER TABLE ""charity"" because it has pending trigger events}}}

I can provide full traceback if necessary; I just didn't want to provide excess on here.

From what I have found, this is because the field is being both altered and updated inside the same transaction, as per this SO answer: http://stackoverflow.com/a/12838113/1971587

Knowing this, I know that I need to create an intermediate migration in order to set defaults on existing rows prior to the alter. However, I don't believe that it should have been possible to create a migration which is inherently broken.

When creating the migration, I don't believe that I should have been presented with the first option here:

{{{
You are trying to change the nullable field 'name_legal' on charity to non-nullable without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
 1) Provide a one-off default now (will be set on all existing rows)
 2) Ignore for now, and let me handle existing rows with NULL myself (e.g. adding a RunPython or RunSQL operation in the new migration file before the AlterField operation)
 3) Quit, and let me add a default in models.py
Select an option: 1
Please enter the default value now, as valid Python
The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now()
>>> ''
}}}"	New feature	closed	Core (System checks)	dev	Normal	fixed		Shai Berger Simon Charette	Ready for checkin	1	0	0	0	0	0
