klen / peewee_migrate

Simple migration engine for Peewee

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bug report: Not work migrate on foreign_keys=1

alteralt opened this issue · comments

Describe the bug
Error in migration execution

To Reproduce
Steps to reproduce the behavior:

  1. Write and run code:
import peewee
import peewee_migrate


db = peewee.SqliteDatabase(":memory:", pragmas={"foreign_keys": 1})


class User(peewee.Model):
    class Meta:
        database = db


class TgAccount(peewee.Model):
    owner = peewee.ForeignKeyField(User)

    class Meta:
        database = db


for model in [User, TgAccount]:
    model.create_table()


user = User.create()

TgAccount.create(owner=user)


migrator = peewee_migrate.Migrator(db)
migrator.add_fields(
    User, name=peewee.CharField(default="John")
)
migrator.run()

Expected behavior
Migration will be executed

Screenshots
image

Desktop (please complete the following information):

  • peewee-migrate 1.6.5
  • peewee 3.15.4
  • python 3.11

Additional context
I understand this is happening because of foreign_keys=1.
If this value is removed, the migration will be completed successfully

Perhaps it should be done so that the library automatically changes foreign_keys to 0 when using SQLiteDatabase?

I fixed it in my code like this. Probably not the best solution

image