0

I'm trying to use peewee's migration module to rename a column from name to title in my page table. However I'm running this confusing error:

peewee.OperationalError: table page__tmp__ has no column named FOREIGN

My guess is that it has something to do with the need to create an intermediary table when using sqlite.


Current Model:

Full source is here 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')

Migration Script:

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')
    )
csytan
  • 188
  • 2
  • 12

1 Answers1

0

I ended up doing it manually with the sqlite3 command line tool:

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;
csytan
  • 188
  • 2
  • 12