I currently have this:
class Committee(models.Model):
# ...some fields...
committee_xml_id = models.IntegerField()
I need to make the field committee_xml_id
unique, i.e. make it so:
class Committee(models.Model):
# ...some fields...
committee_xml_id = models.IntegerField(unique=True)
I have also tried making it so:
class Committee(models.Model):
# ...some fields...
committee_xml_id = models.IntegerField(unique=True, db_index=False)
Alas, the result is the same.
Having run ./manage.py makemigrations
and subsequently, ./manage.py migrate
, the problem is this:
django.db.utils.OperationalError: (1061, "Duplicate key name 'appname_committee_committee_xml_id_d1210032_uniq'")
At first glance, it seems like the problem is that there is already non-unique data in the table, but the problem is precisely that there isn't. There are only 45 rows in the table, and the field committee_xml_id
contains only unique entries.
The following query gives no results, as expected when there are no duplicates:
SELECT
com.committee_xml_id,
COUNT(*)
FROM
appname_committee AS com
GROUP BY
com.committee_xml_id
HAVING
COUNT(*) != 1
For rigor, here is the same query without the HAVING
-condition, showing clearly that there are indeed no duplicates:
SELECT
com.committee_xml_id,
COUNT(*)
FROM
appname_committee AS com
GROUP BY
com.committee_xml_id
Result is:
# committee_xml_id, COUNT(*)
78, 1
79, 1
124, 1
125, 1
129, 1
130, 1
131, 1
132, 1
133, 1
134, 1
137, 1
139, 1
140, 1
141, 1
142, 1
147, 1
148, 1
149, 1
150, 1
151, 1
152, 1
153, 1
154, 1
160, 1
166, 1
167, 1
168, 1
169, 1
170, 1
176, 1
192, 1
193, 1
194, 1
195, 1
198, 1
199, 1
200, 1
201, 1
202, 1
203, 1
204, 1
205, 1
206, 1
207, 1
216, 1
Any help greatly appreciated.