Django-Postgres完整性错误:重复密钥---如何修复?

HdN*_*dN8 2 django postgresql integrity

我在添加模型的新实例时遇到完整性错误,这里是追溯:

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"
Run Code Online (Sandbox Code Playgroud)

我认为密钥的自动增量与实例id不同(根据其他帖子猜测),但我不确定如何修复它.任何帮助将非常感激!

编辑:询问模型,这里是错误源自的模型.然而它已经运行了一段时间没有任何问题,所以它不太可能是由于模型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
Run Code Online (Sandbox Code Playgroud)

mes*_*ntz 8

我之前在加载数据库备份时遇到过这种情况.不知道为什么postgres没有正确更新序列 - 可能遇到了我没看到的错误 - 但我用过的解决方案是检查主键字段中的最大值,然后从中选择附加到它的序列(手动)以使序列在正确的位置.

您可以从命令行client(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 <result of above>;
Run Code Online (Sandbox Code Playgroud)

刚刚意识到,你可能也想看看序列中已有的内容,所以我也在上面添加了一个选择.看看last_value条目.