peewee migration“没有名为FOREIGN的专栏”

时间:2014-06-22 16:31:43

标签: sqlite peewee

我尝试使用peewee的迁移模块将name中的列重命名为title表中的page。但是,我正在运行这个令人困惑的错误:

peewee.OperationalError: table page__tmp__ has no column named FOREIGN

我的猜测是,它与使用sqlite时创建中间表的需要有关。


当前型号:

完整来源是https://github.com/csytan/textmug/blob/master/db.py

class User(BaseModel):
    id = peewee.CharField(primary_key=True, max_length=20)

class Page(BaseModel):
    id = peewee.PrimaryKeyField()
    name = peewee.TextField(unique=True, null=True)
    user = peewee.ForeignKeyField(User, null=True, related_name='pages')

迁移脚本:

from playhouse import migrate

my_db = migrate.SqliteDatabase('database')
migrator = migrate.SqliteMigrator(my_db)

with my_db.transaction():
    migrate.migrate(
        migrator.rename_column('page', 'name', 'title')
    )

1 个答案:

答案 0 :(得分:0)

我最终使用sqlite3命令行工具手动完成:

BEGIN;

ALTER TABLE "page" RENAME TO "page_tmp";

CREATE TABLE "page" ("id" INTEGER NOT NULL PRIMARY KEY, "title" TEXT, "user_id" TEXT, "created" DATETIME NOT NULL, "text" TEXT NOT NULL, "public" SMALLINT NOT NULL, "encrypted" SMALLINT NOT NULL, FOREIGN KEY ("user_id") REFERENCES "user" ("id"));

INSERT INTO "page" SELECT "id","name","user_id","created","text","public","encrypted" FROM "page_tmp";

DROP TABLE "page_tmp";

COMMIT;