Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#24892 closed Bug (fixed)

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

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)

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".

I use Django 1.8.2.

Attachments (1)

testapp.tar.gz (2.6 KB ) - added by Jacek Bzdak 9 years ago.
Application that reproduces this problem.

Download all attachments as: .zip

Change History (6)

by Jacek Bzdak, 9 years ago

Attachment: testapp.tar.gz added

Application that reproduces this problem.

comment:1 by Jacek Bzdak, 9 years ago

Description: modified (diff)

comment:2 by Tim Graham, 9 years ago

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

comment:3 by Tim Graham, 9 years ago

Has patch: set

comment:4 by Tim Graham <timograham@…>, 9 years ago

Resolution: fixed
Status: assignedclosed

In 5ab86809:

Fixed #24892 -- Fixed quoting of SQL when renaming a field to AutoField in PostgreSQL

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

In 8911d2e2:

[1.8.x] Fixed #24892 -- Fixed quoting of SQL when renaming a field to AutoField in PostgreSQL

Backport of 5ab86809832726957dd6f0eb8e17a461f0a9be84 from master

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