python数据库迁移命令_django数据库迁移sqlmigrate调试

django <1.7使用第三方south做数据库迁移,>=1.7将south整合到django里,因此自带数据库迁移功能。这里是>=1.7的数据库迁移。

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

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;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值