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

prefetch causes fields to be added in dirty_fields #2091

Closed
navolotsky opened this issue Jan 21, 2020 · 5 comments
Closed

prefetch causes fields to be added in dirty_fields #2091

navolotsky opened this issue Jan 21, 2020 · 5 comments

Comments

@navolotsky
Copy link

prefetch causes fields related to prefetched models to be added in dirty_fields.

Could you say why this is so? Is it not possible to make another implementation?

Thanks so much for the answer and excellent ORM.

@navolotsky
Copy link
Author

@coleifer Please check it. I got this behavior again on Peewee 3.13.3, Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32.

@coleifer
Copy link
Owner

The regression test is passing fine for me. If you add a failing test, I will look into this again.

@navolotsky
Copy link
Author

from peewee import CharField, ForeignKeyField, Model, SqliteDatabase

database = SqliteDatabase(":memory:")


class A(Model):
    class Meta:
        database = database
        only_save_dirty = True
    some_str = CharField()


class B(Model):
    class Meta:
        database = database
        only_save_dirty = True
    another_str = CharField()
    a_instance = ForeignKeyField(model=A)


if __name__ == "__main__":
    database.create_tables(models=[A, B])
    a = A.create(some_str='s1')
    B.create(another_str='s2', a_instance=a)
    b1 = B.select().where(B.another_str == 's2').execute()[0]
    print(f"Without `prefetch`, dirty fields: {b1.dirty_fields}")
    b2 = B.select().where(B.another_str == 's2').prefetch(A)[0]
    print(f"With `prefetch`, dirty fields: {b2.dirty_fields}")

изображение

@coleifer
Copy link
Owner

coleifer commented May 19, 2020

OP DELIVERS. Thank you!

@coleifer coleifer reopened this May 19, 2020
@coleifer
Copy link
Owner

Hey - I think I see the problem. There's absolutely no reason to use prefetch when going from a child-model to a parent-model, as SQL already handles that. Just do:

query = B.select(B, A).join(A).where(B.another_str == 's2')

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

2 participants