问题描述
在添加模型的新实例时,我得到一个完整性错误,这里是追溯:
追溯:
/ pre>
文件/home/robain/webapps/django/lib/python2.6/django/core/handlers/base.pyin get_response
100. response = callback(request,* callback_args,** callback_kwargs)
文件/home/robain/webapps/django/lib/python2.6/django/contrib/admin/views/decorators.pyin _checklogin
33. return view_func(request,* args,** kwargs)
文件/home/robain/webapps/django/tmanage/tempManage/src/CTmanage.pyin addCT
101. CT.save()
文件/ home / robain /webapps/django/lib/python2.6/django/db/models/base.py在save
434. self.save_base(using = using,force_insert = force_insert,force_update = force_update)
文件/home/robain/webapps/django/lib/python2.6/django/db/models/base.py在save_base
527. result = manager._insert(values,return_id = update_pk,using = usin g)
文件/home/robain/webapps/django/lib/python2.6/django/db/models/manager.py在_insert
195. return insert_query(self.model,values, ** kwargs)
文件/home/robain/webapps/django/lib/python2.6/django/db/models/query.py在insert_query
1479. return query.get_compiler(using =使用).execute_sql(return_id)
在execute_sql
中的文件/home/robain/webapps/django/lib/python2.6/django/db/models/sql/compiler.py783. cursor = super(SQLInsertCompiler,self).execute_sql(无)
文件/home/robain/webapps/django/lib/python2.6/django/db/models/sql/compiler.py在execute_sql
727. cursor.execute(sql,params)
执行
中的/home/robain/webapps/django/lib/python2.6/django/db/backends/util.py文件15.返回self.cursor.execute(sql,params)
文件/home/robain/webapps/django/lib/python2.6/django/db/backends/postgresql_psycopg2/base.py中执行
44 。return self.cursor.execute(query,args)
异常类型:IntegrityError at / tempManage / addCT / 13/34 /
异常值:重复键值违反唯一约束tempManage_childtemplate_previewPath_key
我认为键的自动增量与实例ID(基于其他帖子的猜测)不同步,但我不知道如何解决它。任何帮助将非常感谢!
编辑:询问模型,这里是错误源自的模型。但是它已经运行了一段时间没有任何问题,所以它不太可能在模型中引起...
class ChildTemplate(models.Model):
def get_CTswf_path(self,filename):
return os.path.join('swf','Company_'+ str self.father.company.id),FT_+ str(self.father.id),'CT_'+ str(self.id),filename)
father = models.ForeignKey('父模板',unique = False,verbose_name ='父',blank = True,null = True)
childName = models.CharField(max_length = 20,blank = False,verbose_name ='Child Name')
location = models.ForeignKey(Location,unique = False,blank = True,null = True)
company = models.ForeignKey(Company,unique = False,blank = True,null = True)
isCorporate = models.BooleanField(blank = False,verbose_name ='Corporate')
templatePath = models.FileField(upload_to = get_CTswf_path,verbose_name ='Path',blank = True,null = True)
previewPath = models。 CharField(MAX_LENGTH = 200,blank = True,unique = True)
#migrateTest = models.BooleanField()
def __unicode __(self):
return self.childName
解决方案在加载数据库备份之前,我发生了这种情况。不知道为什么postgres没有得到正确的序列到最新 - 可能遇到一个我没有看到的错误 - 但是我使用的解决方案是检查主键字段中的最大值,然后从您可以从命令行客户端(<$ c $)中列出数据库中的所有序列(<手动)以在正确的位置获取该序列。
c> psql )与
\ds
。可能有一个命名为tempManage_childtemplate_previewPath_id_seq
。
这样的东西应该可以工作。备份您的数据库,然后手动更改它!
SELECT max(id)FROM< model_table> ;;
SELECT * FROM tempManage_childtemplate_previewPath_id_seq;
ALTER SEQUENCE tempManage_childtemplate_previewPath_id_seq RESTART WITH<以上结果> ;;
只是意识到,你可能还想看看序列中已经有什么,所以我添加了也选择上面。查看
last_value
条目。Im getting an Integrity Error when adding a new instance of a model, here's the traceback:
Traceback: File "/home/robain/webapps/django/lib/python2.6/django/core/handlers/base.py" in get_response 100. response = callback(request, *callback_args, **callback_kwargs) File "/home/robain/webapps/django/lib/python2.6/django/contrib/admin/views/decorators.py" in _checklogin 33. return view_func(request, *args, **kwargs) File "/home/robain/webapps/django/tmanage/tempManage/src/CTmanage.py" in addCT 101. CT.save() File "/home/robain/webapps/django/lib/python2.6/django/db/models/base.py" in save 434. self.save_base(using=using, force_insert=force_insert, force_update=force_update) File "/home/robain/webapps/django/lib/python2.6/django/db/models/base.py" in save_base 527. result = manager._insert(values, return_id=update_pk, using=using) File "/home/robain/webapps/django/lib/python2.6/django/db/models/manager.py" in _insert 195. return insert_query(self.model, values, **kwargs) File "/home/robain/webapps/django/lib/python2.6/django/db/models/query.py" in insert_query 1479. return query.get_compiler(using=using).execute_sql(return_id) File "/home/robain/webapps/django/lib/python2.6/django/db/models/sql/compiler.py" in execute_sql 783. cursor = super(SQLInsertCompiler, self).execute_sql(None) File "/home/robain/webapps/django/lib/python2.6/django/db/models/sql/compiler.py" in execute_sql 727. cursor.execute(sql, params) File "/home/robain/webapps/django/lib/python2.6/django/db/backends/util.py" in execute 15. return self.cursor.execute(sql, params) File "/home/robain/webapps/django/lib/python2.6/django/db/backends/postgresql_psycopg2/base.py" in execute 44. return self.cursor.execute(query, args) Exception Type: IntegrityError at /tempManage/addCT/13/34/ Exception Value: duplicate key value violates unique constraint "tempManage_childtemplate_previewPath_key"
I think the autoincrement of the key is out of sync with the instance id's (guess based on other posts), but I'm not sure how to fix it. Any help would be much appreciated!
EDIT: asked for models, here is the model where the error stems from. However it has been running for some time without any problem, so it's unlikely it's caused in the model def...
class ChildTemplate(models.Model):
def get_CTswf_path(self, filename): return os.path.join('swf', 'Company_' + str(self.father.company.id), "FT_" + str(self.father.id), 'CT_' + str(self.id), filename) father = models.ForeignKey('FatherTemplate', unique=False, verbose_name='Father', blank=True, null=True) childName = models.CharField(max_length=20, blank=False, verbose_name='Child Name') location = models.ForeignKey(Location, unique=False, blank=True, null=True) company = models.ForeignKey(Company, unique=False, blank=True, null=True) isCorporate = models.BooleanField(blank=False, verbose_name='Corporate') templatePath = models.FileField(upload_to=get_CTswf_path, verbose_name='Path', blank=True, null=True) previewPath = models.CharField(max_length=200, blank=True, unique=True) #migrateTest = models.BooleanField() def __unicode__(self): return self.childName
解决方案I've had this happen when loading backups of databases before. Have no idea why postgres didn't get the sequence properly up to date - probably ran into an error I didn't see - but the solution I've used was to check the maximum value in the primary key field, and then select from the sequence attached to it (manually) to get that sequence in the right place.
You can list all sequences in the database from the command line client (
psql
) with\ds
. There's probably one named something liketempManage_childtemplate_previewPath_id_seq
.Something like this should work. Back up your database before manually altering it!
SELECT max(id) FROM <model_table>; SELECT * FROM tempManage_childtemplate_previewPath_id_seq; ALTER SEQUENCE tempManage_childtemplate_previewPath_id_seq RESTART WITH <result of above>;
Just realized, you probably also want to see what's already in the sequence, so I've added a select above too. Check out the
last_value
entry.这篇关于Django-Postgres完整性错误:重复键---如何修复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!