码迷,mamicode.com
首页 > 数据库 > 详细

django数据库迁移sqlmigrate调试

时间:2014-12-12 15:05:22      阅读:372      评论:0      收藏:0      [点我收藏+]

标签:io   ar   os   使用   sp   for   on   文件   数据   

django>=1.7数据库迁移只有三个命令

migrate,用来迁移数据库。

用法:migrate app

makemigrations,用来检测数据库变更和生成数据库迁移文件。

用法:makemigratioins app

sqlmigrate,用来把数据库迁移文件转换成数据库语言(displays the SQL statements for a migratioin.)

用法:sqlmigrate app migration,比如makemigrations生成了0001_initial.py,就用sqlmigrate app 0001_intial,这里0001_initial就是migration参数。

一般如果某次migration使用sqlmigrate没有提示错误,那么在migrate时就能成功。

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

如果是失败:

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;


django数据库迁移sqlmigrate调试

标签:io   ar   os   使用   sp   for   on   文件   数据   

原文地址:http://my.oschina.net/u/862582/blog/355421

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!