[Django] #24893: Django migrations don't add unique constraint for field that is changed from primary_key to unique

59 views
Skip to first unread message

Django

unread,
Jun 1, 2015, 3:47:14 PM6/1/15
to django-...@googlegroups.com
#24893: Django migrations don't add unique constraint for field that is changed
from primary_key to unique
----------------------------+--------------------
Reporter: jbzdak | 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
----------------------------+--------------------
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`.

--
Ticket URL: <https://code.djangoproject.com/ticket/24893>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 1, 2015, 3:50:18 PM6/1/15
to django-...@googlegroups.com
#24893: Django migrations don't add unique constraint for field that is changed
from primary_key to unique
----------------------------+--------------------------------------

Reporter: jbzdak | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------+--------------------------------------
Changes (by jbzdak):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Old description:

New description:

Copy-pasted description of problem:

This can be worked-around by generating another migration with two


`migrations.AlterField` one that sets `unique=False` and another that sets
`unique=True`.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/24893#comment:1>

Django

unread,
Jun 1, 2015, 6:26:27 PM6/1/15
to django-...@googlegroups.com
#24893: Django migrations don't add unique constraint for field that is changed
from primary_key to unique
---------------------------------+-------------------------------------
Reporter: jbzdak | Owner: timgraham
Type: Bug | Status: assigned
Component: Migrations | Version: 1.8
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+-------------------------------------
Changes (by timgraham):

* owner: nobody => timgraham
* status: new => assigned
* severity: Normal => Release blocker
* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/24893#comment:2>

Django

unread,
Jun 1, 2015, 8:37:43 PM6/1/15
to django-...@googlegroups.com
#24893: Django migrations don't add unique constraint for field that is changed
from primary_key to unique
---------------------------------+-------------------------------------
Reporter: jbzdak | Owner: timgraham
Type: Bug | Status: assigned
Component: Migrations | Version: 1.8

Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+-------------------------------------
Changes (by timgraham):

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/4733 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/24893#comment:3>

Django

unread,
Jun 2, 2015, 9:32:12 AM6/2/15
to django-...@googlegroups.com
#24893: Django migrations don't add unique constraint for field that is changed
from primary_key to unique
---------------------------------+-------------------------------------
Reporter: jbzdak | Owner: timgraham
Type: Bug | Status: closed
Component: Migrations | Version: 1.8
Severity: Release blocker | Resolution: fixed

Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"e1e6399c2ce39a3656155c4d704340eac83926a7" e1e6399]:
{{{
#!CommitTicketReference repository=""
revision="e1e6399c2ce39a3656155c4d704340eac83926a7"
Fixed #24893 -- Fixed lack of unique constraint when changing a field from
primary_key=True to unique=True
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24893#comment:4>

Django

unread,
Jun 2, 2015, 9:32:49 AM6/2/15
to django-...@googlegroups.com
#24893: Django migrations don't add unique constraint for field that is changed
from primary_key to unique
---------------------------------+-------------------------------------
Reporter: jbzdak | Owner: timgraham
Type: Bug | Status: closed
Component: Migrations | Version: 1.8

Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"1c57d7e7faaeadb7f32e01586da7997d11df1ad9" 1c57d7e]:
{{{
#!CommitTicketReference repository=""
revision="1c57d7e7faaeadb7f32e01586da7997d11df1ad9"
[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
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24893#comment:5>

Reply all
Reply to author
Forward
0 new messages