Opened 10 years ago

Closed 10 years ago

#23505 closed Bug (needsinfo)

unique constraint tracking lost after DropColumn + AddColumn in postgres

Reported by: Andrew Fink Owned by: nobody
Component: Migrations Version: 1.7
Severity: Normal Keywords: unique_together
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Reproduce:

  1. Create model with a multi-column uniqueness constraint in postgres
  2. Drop a column in the uniqueness constraint, then readd it with a different type.

e.g:

foo = models.IntegerField()
# to
foo = models.CharField(max_length=255)
  1. Attempt to modify the uniqueness constraint and run the subsequent migration. It fails with 'ValueError: Found wrong number (0) of constraints for <tablename>'

This is likely due to django not tracking postgres automatically dropping the uniqueness constraint when a column is deleted/added.
See 'Description -> DROP COLUMN':
http://www.postgresql.org/docs/9.1/static/sql-altertable.html

Change History (2)

comment:1 by Baptiste Mispelon, 10 years ago

Hi,

Could you give us some more details on how to reproduce the issue?

I tried using this model:

class Foo(models.Model):
    foo = models.IntegerField()
    bar = models.IntegerField()

    class Meta:
        unique_together = (('foo', 'bar'),)

Then I ran makemigrations and migrate.
After that, I changed the bar field to bar = models.CharField(max_length=10) and ran makemigrations and migrate again.

But when I do that, I can see that the unique constraint is still there:

                                Table "public.bug23505_foo"
 Column |         Type          |                         Modifiers                         
--------+-----------------------+-----------------------------------------------------------
 id     | integer               | not null default nextval('bug23505_foo_id_seq'::regclass)
 foo    | integer               | not null
 bar    | character varying(10) | not null
Indexes:
    "bug23505_foo_pkey" PRIMARY KEY, btree (id)
    "bug23505_foo_foo_30361d88471c726e_uniq" UNIQUE CONSTRAINT, btree (foo, bar)

Thanks.

comment:2 by Tim Graham, 10 years ago

Resolution: needsinfo
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top