Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#24893 closed Bug (fixed)

Django migrations don't add unique constraint for field that is changed from primary_key to unique

Reported by: Jacek Bzdak Owned by: Tim Graham
Component: Migrations Version: 1.8
Severity: Release blocker Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Jacek Bzdak)

This is related to #24892 and uses the same code (I have attached sample project there)

Copy-pasted description of problem:

To cut long story short: in a small application I had models using domain level primary keys, that were strings, I wanted to introduce synthetic primary keys.

Here are models before migration (I have updated int_pk to be unique).

class Foo(models.Model):

  string_pk = models.CharField(
    max_length=10,
    primary_key= True
  )

  int_pk = models.IntegerField(
    null=True
  )

  class Meta:

    db_table = "FOO"

Models after migration:

class Foo(models.Model):

  string_pk = models.CharField(
    max_length=10,
    unique = True
  )

  int_pk = models.AutoField(
    primary_key=True
  )

  class Meta:

    db_table = "FOO"

Generated migration:

class Migration(migrations.Migration):

    dependencies = [
        ('testapp', '0002_foo_int_pk'),
    ]

    operations = [
        migrations.AlterField(
            model_name='foo',
            name='int_pk',
            field=models.AutoField(primary_key=True, serialize=False),
        ),
        migrations.AlterField(
            model_name='foo',
            name='string_pk',
            field=models.CharField(max_length=10, unique=True),
        ),
    ]

I have changed field string_pk from primary_key=True to unique=True, generated migration (when I manually fixed error from #24892) didn't create unique constraint (previously there were a primary key constraint, that was dropped but there were no unique key generated).

This can be worked-around by generating another migration with two migrations.AlterField one that sets unique=False and another that sets unique=True.

Change History (5)

comment:1 Changed 5 years ago by Jacek Bzdak

Description: modified (diff)

comment:2 Changed 5 years ago by Tim Graham

Owner: changed from nobody to Tim Graham
Severity: NormalRelease blocker
Status: newassigned
Triage Stage: UnreviewedAccepted

comment:3 Changed 5 years ago by Tim Graham

Has patch: set

comment:4 Changed 5 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In e1e6399:

Fixed #24893 -- Fixed lack of unique constraint when changing a field from primary_key=True to unique=True

comment:5 Changed 5 years ago by Tim Graham <timograham@…>

In 1c57d7e:

[1.8.x] Fixed #24893 -- Fixed lack of unique constraint when changing a field from primary_key=True to unique=True

Backport of e1e6399c2ce39a3656155c4d704340eac83926a7 from master

Note: See TracTickets for help on using tickets.
Back to Top