#25492: makemigrations can generate invalid migrations when removing null=True
from
a field
-----------------------------+--------------------
Reporter: tarkatronic | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.8
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+--------------------
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()
>>> ''
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/25492>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/054.e83a3cb278444f9cf32d5718ecbf2a4b%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.