Opened 4 years ago

Closed 4 years ago

#31688 closed Bug (duplicate)

Fail to rename and reuse field when using postgres

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

Description

The scenario is the following.

  1. model with foreign key pointer point to Model1
  2. pointer in rename to old_pointer ( + making rename migration)
  3. adding pointer again but not points to Model2

getting an error that index is already exists.

This fails on Postgres, when working with sqlite it is ok.

Below is the relevant code:

Step 1:

class Model1(models.Model):
    name = models.CharField(max_length=30)


class Model2(models.Model):
    name2 = models.CharField(max_length=30)


class MainModel(models.Model):
    pointer = models.ForeignKey(Model1, null=True, on_delete=models.CASCADE)

Then running makemigrations + migrate.

Step 2:

Changing the pointer to old_pointer then running makemigrations + migrate (+ yes for the rename question)

class MainModel(models.Model):
    old_pointer = models.ForeignKey(Model1, null=True, on_delete=models.CASCADE)

Step 3:
Adding back the pointer field, now to Model2

class MainModel(models.Model):
    old_pointer = models.ForeignKey(Model1, null=True, on_delete=models.CASCADE)
    pointer = models.ForeignKey(Model2, null=True, on_delete=models.CASCADE)

running makemigrations and then migrate yields the following error:

return self.cursor.execute(sql, params)
psycopg2.errors.DuplicateTable: relation "app1_mainmodel_pointer_id_c7a78539" already exists

Change History (1)

comment:1 by Mariusz Felisiak, 4 years ago

Component: UncategorizedMigrations
Resolution: duplicate
Status: newclosed
Type: UncategorizedBug

Duplicate of #23577.

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