1

I have a db with the following Django Migrations:

0001_initial.py:

migrations.AddField(
    model_name='extrashirtprice',
    name='sibs_weekend',
    field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='registration.SibsWeekend'),
),

So I want to make it a OneToOneField instead. So there's a new migration:

0012_auto.py:

operations = [
    migrations.AlterField(
        model_name='extrashirtprice',
        name='sibs_weekend',
        field=models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to='registration.SibsWeekend'),
    ),
]

I checked and ensured that the operation done in 0012 wasn't done anywhere else.

When I run migrate, I get:

django.db.utils.ProgrammingError: relation "registration_extrashirtprice_sibs_weekend_id_6626038b_uniq" already exists

I looked at Django - Change a ForeignKey relation to OneToOne but they were talking about using --fake which would not actually apply the unique constraint. Plus we deploy with an automated script so I want to avoid adding commands if possible. This post also had no actual accepted answers and it was in the context of south migrations so I'd rather see how it can be done using Django migrations in a better way.

I could just use ForeignKey with a unique constraint (I assume) but I want to use the more official OneToOneField for this if possible.

NOTE: I am running these migrations on a database with data. It's not empty so if the issue has anything to do with the fact that there's data, please show me how to fix that since I don't want to lose production data.

Also, I am assuming in this that a OneToOneField is just a ForeignKey with a unique constraint. Please correct me if I'm wrong cause otherwise, this whole question is invalid.

Ian Kirkpatrick
  • 1,861
  • 14
  • 33
  • Which version of Django are you using? The database schema is the same for a `OneToOneField` and a `ForeignKey` with a unique constraint, however there are some changes in the Django API - see [this question](https://stackoverflow.com/questions/5870537/whats-the-difference-between-django-onetoonefield-and-foreignkey) for examples. – Alasdair Apr 09 '18 at 14:05
  • I'm using Django 1.11.2 – Ian Kirkpatrick Apr 09 '18 at 14:06
  • What do you mean by "but doesn't really" in the title? Did you check that no current object breaks the one-to-one relationship, so adding the unique constraint can be done? – Paulo Almeida Apr 09 '18 at 14:07
  • @PauloAlmeida, I meant that, after inspecting the database and migration history, the unique constraint does not already exist but the error says it does. As for checking the data, I'm checking that right now... – Ian Kirkpatrick Apr 09 '18 at 14:11
  • There is no data that would violate the constraint so the issue is not the specific data itself. – Ian Kirkpatrick Apr 09 '18 at 14:16
  • There's [a comment](https://stackoverflow.com/questions/7341722/django-change-a-foreignkey-relation-to-onetoone#comment58699191_8277945) in the question you linked to that did this in 2016, with native migrations and MySQL, and it just worked. I tried it now with SQLite (Django 2) and it also worked, failing with an `IntegrityError` if there are objects violating the constraint. Do any of the other 10 migrations touch that field in any way (is there an index, for instance)? – Paulo Almeida Apr 09 '18 at 17:10

0 Answers0