diadia

興味があることをやってみる。自分のメモを残しておきます。

IntegrityErrorが発生してしまう。。。

エラー対処中 解決次第更新する

 django.db.utils.IntegrityError: NOT NULL constraint failed: appname_model.anoteher_model_id

同じエラーが出ている人の記事:

http://nihaoshijie.hatenadiary.jp/entry/2014/06/12/090008 

ドキュメントのフィールドオプション、ユニークについて

https://docs.djangoproject.com/ja/2.1/ref/models/fields/#unique

True の場合、そのフィールドはテーブル上で一意となる制約を受けます。 This is enforced at the database level and by model validation. If you try to save a model with a duplicate value in a unique field, a django.db.IntegrityError will be raised by the model's save() method. This option is valid on all field types except ManyToManyField and OneToOneField. Note that when unique is True, you don't need to specify db_index, because unique implies the creation of an index.

 

uniqueフィールドにダブった値を入れてsave()しようとすると、django.db.IntegrityErrorが生じる。と書いてある。

エラー対処できた。自分の原因はやはりuniqueオプションに関係していた。あるモデルのuniqueフィールドに特定のデータを入れたインスタンスがすでに存在しているにもかかわらず、uniqueフィールドに同じデータを別のインスタンスとして作成や保存しようとするコードになっていた。それが原因だった。