Somehow a table in my PostgreSQL database had every row duplicated, so there are now two rows for every id (primary key -- see attached sample image below). Another column in that table, FAN
is a foreign key in another table holdings
. When I tried to delete the duplicates in table surveys
with:
DELETE
FROM
surveys a
USING surveys b
WHERE
a.id = b.id;
I got the error:
ERROR: update or delete on table "surveys" violates foreign key constraint holdings_FAN_id_ca1342a3_fk_surveys_id" on table "holdings"
I tried the solution to this SO question, but the the output was just an empty id
column (no rows, no values), and the original surveys
table was unchanged. I'm new to SQL and don't fully understand what was going on in those commands, so I may have missed something.
How can I remove the duplicate rows in my table?
Using PostgreSQL 9.6.10. Tables were created with Django models (sample code added based on comment):
class surveys(models.Model):
FAN = models.SlugField(max_length=100, blank=True, null=True)
class holdings(models.Model):
FAN = models.ForeignKey('surveys', on_delete=models.SET_NULL, blank=True, null=True)