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

Order of join clause with alias join and select can lead to invalid configurations #438

Closed
jakedt opened this issue Sep 24, 2014 · 4 comments

Comments

@jakedt
Copy link

jakedt commented Sep 24, 2014

I'm not sure how to describe the problem accurately, so here is a repro case:

from peewee import Model, SqliteDatabase, CharField, ForeignKeyField, create_model_tables

db = SqliteDatabase(':memory:')

class Person(Model):
    name = CharField(default='aperson')
    class Meta:
        database = db

class RelatesToPerson(Model):
    name = CharField(default='arelationship')
    person = ForeignKeyField(Person)
    class Meta:
        database = db

create_model_tables([Person, RelatesToPerson])

PersonAlias = Person.alias()

a_person = Person.create()
RelatesToPerson.create(person=a_person)

print str(RelatesToPerson
          .select(RelatesToPerson, PersonAlias)
          .join(PersonAlias, on=(PersonAlias.id == RelatesToPerson.person))
          .get().id) + " <- an instance!"

print (RelatesToPerson
       .select(RelatesToPerson, PersonAlias)
       .join(PersonAlias, on=(RelatesToPerson.person == PersonAlias.id))
       .get().id)

output

$ python repro.py
<__main__.Person object at 0x10b201c50> <- an instance!
1
@coleifer
Copy link
Owner

Thanks for sharing some code to reproduce this!

@coleifer
Copy link
Owner

Basically what peewee is trying to figure out is how to patch the joined instance (PersonAlias, which is actually a Person) onto RelatesToPerson given the join predicate. There's some special-casing around aliases and expressions -- I think there are some undiscovered bugs in the relevant block of code. I'm working on a complete refactor to hopefully clean this up and make the behavior more predictable.

@coleifer
Copy link
Owner

I've got things partially fixed in 5537389

@coleifer
Copy link
Owner

I believe this is now fixed.

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