これらのフィールドをモデルに追加しました:
class WatchList(models.Model):
name = models.CharField(max_length=20)
class Thing(models.Model):
watchlist = models.ForeignKey(WatchList)
スキーマ移行を正常に実行しました:
>>> $ python2.7 manage.py schemamigration myapp --auto
+ Added model myapp.WatchList
? The field 'Thing.watchlist' does not have a default specified, yet is NOT NULL.
? Since you are adding this field, you MUST specify a default
? value to use for existing rows. Would you like to:
? 1. Quit now, and add a default to the field in models.py
? 2. Specify a one-off value to use for existing columns now
? Please select a choice: 2
? Please enter Python code for your one-off default value.
? The datetime module is available, so you can do e.g. datetime.date.today()
>>> 0
+ Added field watchlist on myapp.Thing
Created 0004_auto__add_watchlist__add_field_thing_watchlist.py. You can now apply this migration with: ./manage.py migrate myapp
これを行う前は問題はありませんでしたが、何らかの理由で次のエラーが発生しました。
>>> $ python2.7 manage.py migrate myapp
FATAL ERROR - The following SQL query failed: ALTER TABLE "myapp_thing" ADD CONSTRAINT "watchlist_id_refs_id_1b2eef756112b8e" FOREIGN KEY ("watchlist_id") REFERENCES "myapp_watchlist" ("id") DEFERRABLE INITIALLY DEFERRED;
The error was: insert or update on table "myapp_thing" violates foreign key constraint "watchlist_id_refs_id_1b2eef756112b8e"
DETAIL: Key (watchlist_id)=(0) is not present in table "myapp_watchlist".
モデルへの変更を正常に移行するにはどうすればよいですか? 役立つアイデアをありがとう!