2

モデルの新しいインスタンスを追加するときに整合性エラーが発生します。トレースバックは次のとおりです。

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"

キーの自動インクリメントがインスタンス ID と同期していないと思いますが (他の投稿に基づいて推測します)、修正方法がわかりません。どんな助けでも大歓迎です!

編集:モデルを求められました。エラーの原因となったモデルは次のとおりです。とはいえ、しばらく問題なく動いているので、機種定義に原因があるとは考えにくいのですが…

クラス 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
4

2 に答える 2

9

以前にデータベースのバックアップをロードするときに、これが発生しました。postgres がシーケンスを適切に最新のものにしなかった理由がわかりません - おそらく私が見ていないエラーに遭遇しました - しかし、私が使用した解決策は、主キーフィールドの最大値をチェックしてから選択することでした適切な場所にそのシーケンスを取得するために (手動で) 添付されたシーケンス。

を使用して、コマンド ライン クライアント ( 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>;

おそらく、すでにシーケンスにあるものも見たいと思うので、上にも選択を追加しました。エントリーをご覧くださいlast_value

于 2011-03-29T18:19:56.780 に答える
2

重要なのは次の 2 行だけです。

Exception Value: duplicate key value violates unique constraint "tempManage_childtemplate_previewPath_key"
previewPath = models.CharField(max_length=200, blank=True, unique=True)

previewPath に一意のキーを追加しましたが、既に存在する値を挿入しようとしています。したがって、アプリが重複した preivewPath を挿入しようとしている理由を理解する必要があります。(どこで id out sync を取得しているのかわかりません。)

于 2011-03-29T17:56:26.137 に答える