Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to select/update model after migrating ForeignKeyField: RecursionError #2171

Closed
arel opened this issue May 9, 2020 · 0 comments
Closed

Comments

@arel
Copy link

arel commented May 9, 2020

I have a migration script that first adds a column and then initializes it. I am using MySQL. However, after adding the column, the model's ForeignKeyField is changed in a way that results in a recursion error by simply selecting or updating the new field:

RecursionError: maximum recursion depth exceeded

To reproduce it, imagine we have the two tables below, and I want to add a ForeignKeyField column to the Rice table:

from peewee import Model, ForeignKeyField, TextField

class Bean(Model):
    name = TextField(default="Red")
    class Meta:
        database = db

class Rice(Model):
    name = TextField(default="Brown")
    bean = ForeignKeyField(Bean, null=True, backref='rices')  # <-- we want to add this field
    class Meta:
        database = db

Now, let's say I migrate the Rice table with something like this:

migrator = MySQLMigrator(db)

migrate(
    migrator.add_column(
        Rice._meta.table_name,
        Rice.bean.column_name,
        Rice.bean
    ),
)

As a result of this line:

field.name = field.column_name = column_name
the value, Rice.bean.name, gets changed from "bean" to "bean_id". This causes a RecursionError the next time we select that field.

Rice.select().first()  # FAIL

~/.local/share/virtualenvs/acm-api-UVOmdwT4/lib/python3.7/site-packages/peewee.py in __set__(self, instance, value)
   4380 
   4381     def __set__(self, instance, value):
-> 4382         setattr(instance, self.field.name, value)
   4383 
   4384 

RecursionError: maximum recursion depth exceeded
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant