[Django] #24892: Django migrations don't escape uppercase table name in "" when using postgres backend when changing Integer field to Auto field

5 views
Skip to first unread message

Django

unread,
Jun 1, 2015, 2:59:46 PM6/1/15
to django-...@googlegroups.com
#24892: Django migrations don't escape uppercase table name in "" when using
postgres backend when changing Integer field to Auto field
----------------------------+--------------------
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: 1
----------------------------+--------------------
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),
),
]
}}}

This migration fails with following error:

{{{
django.db.utils.ProgrammingError: relation "foo" does not exist
}}}

Generated SQL is:

{{{
ALTER TABLE "FOO" ALTER COLUMN "int_pk" TYPE integer, ALTER COLUMN
"int_pk" SET NOT NULL;
DROP SEQUENCE IF EXISTS FOO_int_pk_seq CASCADE;
CREATE SEQUENCE FOO_int_pk_seq;
ALTER TABLE FOO ALTER COLUMN int_pk SET DEFAULT nextval('FOO_int_pk_seq');
SELECT setval('FOO_int_pk_seq', MAX(int_pk)) FROM FOO;
ALTER TABLE "FOO" ADD CONSTRAINT "FOO_int_pk_5b283460a20ef820_uniq" UNIQUE
("int_pk");
ALTER TABLE "FOO" DROP CONSTRAINT "FOO_pkey";
ALTER TABLE "FOO" ADD CONSTRAINT "FOO_int_pk_5b283460a20ef820_pk" PRIMARY
KEY ("int_pk");
}}}

Error is caused by wollowing two lines:

{{{
ALTER TABLE FOO ALTER COLUMN int_pk SET DEFAULT nextval('FOO_int_pk_seq');
SELECT setval('FOO_int_pk_seq', MAX(int_pk)) FROM FOO;
}}}
In these lines `FOO` should be replaced by `"FOO"`

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

Django

unread,
Jun 1, 2015, 3:00:51 PM6/1/15
to django-...@googlegroups.com
#24892: Django migrations don't escape uppercase table name in "" when using
postgres backend when changing Integer field to Auto field
------------------------+----------------------------

Reporter: jbzdak | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.8
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Easy pickings: 0
UI/UX: 1 |
------------------------+----------------------------
Changes (by jbzdak):

* Attachment "testapp.tar.gz" added.

Application that reproduces this problem.

Django

unread,
Jun 1, 2015, 3:01:53 PM6/1/15
to django-...@googlegroups.com
#24892: Django migrations don't escape uppercase table name in "" when using
postgres backend when changing Integer field to Auto field
----------------------------+--------------------------------------

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: 1
----------------------------+--------------------------------------
Changes (by jbzdak):

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


Old description:

New description:

{{{
class Foo(models.Model):

class Meta:

db_table = "FOO"

}}}

Models after migration:

{{{
class Foo(models.Model):

class Meta:

db_table = "FOO"
}}}

Generated migration:

{{{

class Migration(migrations.Migration):

Generated SQL is:

In these lines `FOO` should be replaced by `"FOO"`.

I use Django `1.8.2`.

--

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

Django

unread,
Jun 1, 2015, 4:53:22 PM6/1/15
to django-...@googlegroups.com
#24892: Django migrations don't escape uppercase table name in "" when using
postgres backend when changing Integer field to Auto field
---------------------------------+-------------------------------------
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
* ui_ux: 1 => 0
* severity: Normal => Release blocker
* stage: Unreviewed => Accepted


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

Django

unread,
Jun 1, 2015, 5:25:15 PM6/1/15
to django-...@googlegroups.com
#24892: Django migrations don't escape uppercase table name in "" when using
postgres backend when changing Integer field to Auto field
---------------------------------+-------------------------------------
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/4730 PR]

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

Django

unread,
Jun 2, 2015, 9:14:40 AM6/2/15
to django-...@googlegroups.com
#24892: Django migrations don't escape uppercase table name in "" when using
postgres backend when changing Integer field to Auto field
---------------------------------+-------------------------------------
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:"5ab86809832726957dd6f0eb8e17a461f0a9be84" 5ab86809]:
{{{
#!CommitTicketReference repository=""
revision="5ab86809832726957dd6f0eb8e17a461f0a9be84"
Fixed #24892 -- Fixed quoting of SQL when renaming a field to AutoField in
PostgreSQL
}}}

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

Django

unread,
Jun 2, 2015, 9:15:43 AM6/2/15
to django-...@googlegroups.com
#24892: Django migrations don't escape uppercase table name in "" when using
postgres backend when changing Integer field to Auto field
---------------------------------+-------------------------------------
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:"8911d2e20fc5f4fc7b0274594813ed0b1ad8bc6b" 8911d2e2]:
{{{
#!CommitTicketReference repository=""
revision="8911d2e20fc5f4fc7b0274594813ed0b1ad8bc6b"
[1.8.x] Fixed #24892 -- Fixed quoting of SQL when renaming a field to
AutoField in PostgreSQL

Backport of 5ab86809832726957dd6f0eb8e17a461f0a9be84 from master
}}}

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

Reply all
Reply to author
Forward
0 new messages