#27267: Renaming a primary key fails with "cannot drop constraint on table
because
other objects depend on it"
--------------------------------+--------------------------------------
Reporter: Melvyn Sopacua | Owner: nobody
Type: Bug | Status: closed
Component: Migrations | Version: 1.8
Severity: Normal | Resolution: needsinfo
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------------------------
Comment (by Stephan Doliov):
For what it is worth, I encountered the same error.
I am using Django 2.01 and Postgres 9.6
I have a workaround for the problem simply by declaring my migration to be
non-atomic
{{{ atomic = False}}}
Here is what it looks like in practice for me. For certain migrations, I
initialize them with data; if I undo the migration, I also want to clear
out that seed data, in the right order as all my OneToOneFields have
models.PROTECT declared, like so
{{{
guid = models.OneToOneField(Guid, on_delete=models.PROTECT,
primary_key=True)
}}}
so then my migrations looks like
{{{
# Generated by Django 2.0.2 on 2018-03-07 23:52
from django.core import management
from django.db import migrations, models
import django.db.models.deletion
from dim.models import Guid, Role
def create_fixture(apps, schema_editor):
management.call_command('dumpdata', 'dim.Role', format='json',
indent=4,
output='dim/fixtures/000002_dim_role.json'
)
def init_data(apps, schema_editor):
Role.objects.create(guid=Guid.objects.create(), name="Basic Consumer")
Role.objects.create(guid=Guid.objects.create(), name="Basic
Publisher")
def clear_data(apps, schema_editor):
import pdb
pdb.set_trace()
for role in Role.objects.all():
guid = role.guid.guid
role.delete(purge=True)
Guid.objects.get(guid=guid).delete(purge=True)
migrations.RunSQL("drop table dim_role cascade")
class Migration(migrations.Migration):
atomic = False
dependencies = [
('dim', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='Role',
fields=[
('guid',
models.OneToOneField(on_delete=django.db.models.deletion.PROTECT,
primary_key=True, serialize=False, to='dim.Guid')),
('name', models.CharField(max_length=64, unique=True,
verbose_name='name')),
('created_ts', models.DateTimeField(auto_now_add=True)),
('updated_ts', models.DateTimeField(auto_now=True,
null=True)),
('deleted_ts', models.DateTimeField(null=True)),
],
options={
'verbose_name': 'role',
'verbose_name_plural': 'roles',
'db_table': 'dim_role',
},
),
migrations.RunPython(init_data, reverse_code=clear_data),
migrations.RunPython(create_fixture,
reverse_code=migrations.RunPython.noop)
]
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27267#comment:15>
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/072.fd60ae52cb1b6cbdd522a645ad774546%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.