0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Django】UNIQUE constraint failed. のエラーが出たときの原因

Posted at

Djangoで UNIQUE constraint failed エラーが出たときの原因

カスタムユーザモデルのemailフィールドのunique=Trueにした後、
migrateしようとすると↓のエラーが

django.db.utils.IntegrityError: UNIQUE constraint failed: new__users_customuser.email

解決方法

いったんrunserverして、User DBを確認
→emailがすでに重複しているユーザがあったため、1ユーザだけ残してあとは削除
→解決

原因

まれなケースかもしれませんが、
unique=True に変更する前に同じemailでユーザ登録を行ってしまっていたために出たエラーでした

おわり

UNIQUE contraint failed のエラーが出たとき、
unique=Trueにする前に、DBの中にすでに重複したレコードを登録してしまっていないか確認してみてください

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?