klen / peewee_migrate

Simple migration engine for Peewee
MIT License
352 stars 86 forks source link

Bug report: Not work migrate on foreign_keys=1 #218

Open alteralt opened 1 year ago

alteralt commented 1 year ago

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](https://user-images.githubusercontent.com/109542778/210541550-274c7551-f934-41ac-bdf5-6b1babea6a43.png)

**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?
alteralt commented 1 year ago

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

image