django數據庫遷移sqlmigrate調試

django>=1.7數據庫遷移只有三個命令python

migrate,用來遷移數據庫。sql

用法:migrate app數據庫

makemigrations,用來檢測數據庫變動和生成數據庫遷移文件。django

用法:makemigratioins appapp

sqlmigrate,用來把數據庫遷移文件轉換成數據庫語言(displays the SQL statements for a migratioin.)python2.7

用法:sqlmigrate app migration,好比makemigrations生成了0001_initial.py,就用sqlmigrate app 0001_intial,這裏0001_initial就是migration參數。fetch

通常若是某次migration使用sqlmigrate沒有提示錯誤,那麼在migrate時就能成功。3d

migrate成功例子:調試

root@kanfangha:~/redguy# python manage.py migrate accost
Operations to perform:
  Apply all migrations: accost
Running migrations:
  Applying accost.0002_auto_20141212_1327... OK

若是是失敗:code

Operations to perform:
  Apply all migrations: accost
Running migrations:
  Applying accost.0002_auto_20141212_1327... FAKED

因此若是migrate失敗了,能夠用sqlmigrate調試。

好比:

root@kanfangha:~/redguy# python manage.py sqlmigrate accost 0002_auto_20141212_1323
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/sqlmigrate.py", line 30, in execute
    return super(Command, self).execute(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/sqlmigrate.py", line 61, in handle
    sql_statements = executor.collect_sql(plan)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 77, in collect_sql
    migration.apply(project_state, schema_editor, collect_sql=True)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/migration.py", line 107, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/operations/fields.py", line 139, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/schema.py", line 470, in alter_field
    new_field,
ValueError: Cannot alter field accost.Accost.like into accost.Accost.like - they are not compatible types (you cannot alter to or from M2M fields, or add or remove through= on M2M fields)

上面提示說不能將accost.Accost.like alter爲一個M2M(ManyToMany)fields,這種狀況,把字段like換個名字就好了,換成likes。而後在執行sqlmigrate就成功。

以下:

root@kanfangha:~/redguy# python manage.py sqlmigrate accost 0002_auto_20141212_1327
BEGIN;
ALTER TABLE "accost_accost" DROP COLUMN "collected" CASCADE;
ALTER TABLE "accost_accost" DROP COLUMN "like" CASCADE;
CREATE TABLE "accost_accost_collects" ("id" serial NOT NULL PRIMARY KEY, "accost_id" integer NOT NULL, "myuser_id" integer NOT NULL, UNIQUE ("accost_id", "myuser_id"));
CREATE TABLE "accost_accost_likes" ("id" serial NOT NULL PRIMARY KEY, "accost_id" integer NOT NULL, "myuser_id" integer NOT NULL, UNIQUE ("accost_id", "myuser_id"));
CREATE INDEX accost_accost_collects_372770c0 ON "accost_accost_collects" ("accost_id");
ALTER TABLE "accost_accost_collects" ADD CONSTRAINT "accost_accost_co_accost_id_2b66c74632ab3db8_fk_accost_accost_id" FOREIGN KEY ("accost_id") REFERENCES "accost_accost" ("id") DEFERRABLE INITIALLY DEFERRED;
CREATE INDEX accost_accost_collects_8b14fb18 ON "accost_accost_collects" ("myuser_id");
ALTER TABLE "accost_accost_collects" ADD CONSTRAINT "accost_accost__myuser_id_1bf115233bc10d70_fk_accounts_myuser_id" FOREIGN KEY ("myuser_id") REFERENCES "accounts_myuser" ("id") DEFERRABLE INITIALLY DEFERRED;
CREATE INDEX accost_accost_likes_372770c0 ON "accost_accost_likes" ("accost_id");
ALTER TABLE "accost_accost_likes" ADD CONSTRAINT "accost_accost_li_accost_id_3700c980b7e22204_fk_accost_accost_id" FOREIGN KEY ("accost_id") REFERENCES "accost_accost" ("id") DEFERRABLE INITIALLY DEFERRED;
CREATE INDEX accost_accost_likes_8b14fb18 ON "accost_accost_likes" ("myuser_id");
ALTER TABLE "accost_accost_likes" ADD CONSTRAINT "accost_accost__myuser_id_6af76a97d74d7ca4_fk_accounts_myuser_id" FOREIGN KEY ("myuser_id") REFERENCES "accounts_myuser" ("id") DEFERRABLE INITIALLY DEFERRED;
COMMIT;
相關文章
相關標籤/搜索