我目前有这样的:
class Committee(models.Model):
# ...some fields...
committee_xml_id = models.IntegerField()我需要使committee_xml_id字段具有唯一性,即使其成为:
class Committee(models.Model):
# ...some fields...
committee_xml_id = models.IntegerField(unique=True)我也试过这样做:
class Committee(models.Model):
# ...some fields...
committee_xml_id = models.IntegerField(unique=True, db_index=False)唉,结果是一样的。
运行./manage.py makemigrations和./manage.py migrate后,问题是:
django.db.utils.OperationalError: (1061, "Duplicate key name 'appname_committee_committee_xml_id_d1210032_uniq'")乍一看,问题似乎是表中已经有非唯一数据,但问题恰恰在于没有。表中只有45行,并且字段committee_xml_id仅包含唯一条目。
下面的查询没有结果,如预期的那样没有重复:
SELECT
com.committee_xml_id,
COUNT(*)
FROM
appname_committee AS com
GROUP BY
com.committee_xml_id
HAVING
COUNT(*) != 1严格来说,这里是没有HAVING条件的相同查询,清楚地表明确实没有重复:
SELECT
com.committee_xml_id,
COUNT(*)
FROM
appname_committee AS com
GROUP BY
com.committee_xml_id结果是:
# 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任何帮助不胜感激。