Issue: Can not pull data for joined models
Author: kalpesh99
State: closed
Created: 2017-04-01 00:14:33

UserLesson.select(UserLesson, Lesson, User).join(Lesson).switch(UserLesson).join(User).first() query works as expected (returning data for all three classes) but Lesson.select(Lesson, UserLesson, User).join(UserLesson).join(User).first() returns only Lesson class/model for models shown below. Why does it behave like it? I am not sure if this is a right place to ask this kind of question, so forgive me if I made a mistake. Thank you. 

class User(Model):
    id = PrimaryKeyField()
    username = CharField(max_length=32, null=False)
    password = TextField(null=False)
    first_name = CharField(max_length=32, null=True)
    last_name = CharField(max_length=32, null=True)
    email = CharField(max_length=64, null=True)
    class Meta:
        database = db
        db_table = 'user'

class VLesson(Model):
    id = PrimaryKeyField()
    description = TextField()
    title = TextField()
    class Meta:
        database = db
        db_table = 'vlesson'

class TLesson(Model):
    id = PrimaryKeyField()
    description = TextField()
    title = TextField()
    class Meta:
        database = db
        db_table = 'tlesson'

class Lesson(Model):
    id = PrimaryKeyField()
    module = IntegerField()
    tlesson_id = ForeignKeyField(db_column='tlesson_id', null=True, rel_model=TLesson, to_field='id')
    vlesson_id = ForeignKeyField(db_column='vlesson_id', null=True, rel_model=VLesson, to_field='id')
    class Meta:
        database = db
        db_table = 'lesson'

class UserLesson(Model):
    id = PrimaryKeyField()
    user_id   = ForeignKeyField(db_column='user_id',   null=True, rel_model=User,  to_field='id')
    lesson_id = ForeignKeyField(db_column='lesson_id', null=True, rel_model=Lesson, to_field='id')
    class Meta:
        database = db
        db_table = 'user_lesson'



Could you please take the time to format your code?

```python
class User(Model):
    id = PrimaryKeyField()
    username = CharField(max_length=32, null=False)
    password = TextField(null=False)
    first_name = CharField(max_length=32, null=True)
    last_name = CharField(max_length=32, null=True)
    email = CharField(max_length=64, null=True)
    class Meta:
        database = db
        db_table = 'user'

class VLesson(Model):
    id = PrimaryKeyField()
    description = TextField()
    title = TextField()
    class Meta:
        database = db
        db_table = 'vlesson'

class TLesson(Model):
    id = PrimaryKeyField()
    description = TextField()
    title = TextField()
    class Meta:
        database = db
        db_table = 'tlesson'

class Lesson(Model):
    id = PrimaryKeyField()
    module = IntegerField()
    tlesson_id = ForeignKeyField(db_column='tlesson_id', null=True, rel_model=TLesson, to_field='id')
    vlesson_id = ForeignKeyField(db_column='vlesson_id', null=True, rel_model=VLesson, to_field='id')
    class Meta:
        database = db
        db_table = 'lesson'

class UserLesson(Model):
    id = PrimaryKeyField()
    user_id = ForeignKeyField(db_column='user_id', null=True, rel_model=User, to_field='id')
    lesson_id = ForeignKeyField(db_column='lesson_id', null=True, rel_model=Lesson, to_field='id')
    class Meta:
        database = db
        db_table = 'user_lesson'
```

Following query works as expected
```python
UserLesson.select(UserLesson, Lesson, User).join(Lesson).switch(UserLesson).join(User).first() 
```

But following query returns only "Lesson" class/model instead of all three models. For this query I did verify SQL output dumped by peeewee works as expected when tried in SQLite console, so SQL produced by peewee is right but data retrieved from SQLite don't get populated correctly in to classes/models.  
```python
Lesson.select(Lesson, UserLesson, User).join(UserLesson).join(User).first() 
```

Sorry for not formatting code before and thank you for prompt response.

Because a `Lesson` may be associated with more than one `UserLesson`, the way peewee aliases the joins is slightly different.

Did you try accessing the `UserLesson` object like this:

```python
lesson = Lesson.select(Lesson, UserLesson, User).join(UserLesson).join(User).first()
print lesson.userlesson.user_id.username
```

?

userlesson and/or user_lesson attribute not present

```python
>>> lesson = Lesson.select(Lesson, UserLesson).join(UserLesson).first()
>>> lesson.user_lesson
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Lesson' object has no attribute 'user_lesson'
>>> lesson.__dict__
{'_data': {'vlesson_id': None, 'id': 1, 'tlesson_id': 1, 'module': 1}, '_obj_cache': {}, '_dirty': set()}
>>> lesson.userlesson
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Lesson' object has no attribute 'userlesson'
```

But the same query works as expected if I change my model to have foreign key defined on on ```TLesson```, ```VLesson``` instead of on ```Lesson``` as shown below. 
```python

class User(Model):
    id              = PrimaryKeyField()

    username        = CharField(max_length=32, null=False)
    password        = TextField(null=False)

    first_name      = CharField(max_length=32, null=True)
    last_name       = CharField(max_length=32, null=True)
    email           = CharField(max_length=64, null=True)
    class Meta:
        database = main.jdb
        db_table = 'user'

class Lesson(Model):
    id = PrimaryKeyField()
    module = IntegerField()
    lesson = IntegerField()
    class Meta:
        database = main.jdb
        db_table = 'lesson'

class TLesson(Model):
    id = PrimaryKeyField()
    description = TextField()
    title = TextField()
    lesson_id = ForeignKeyField(db_column='lesson_id', null=False, rel_model=Lesson, to_field='id')
    class Meta:
        database = main.jdb
        db_table = 'tlesson'

class VLesson(Model):
    id = PrimaryKeyField()
    description = TextField()
    title = TextField()
    lesson_id = ForeignKeyField(db_column='lesson_id', null=False, rel_model=Lesson, to_field='id')
    class Meta:
        database = main.jdb
        db_table = 'vlesson'

class UserLesson(Model):
    id = PrimaryKeyField()
    user_id = ForeignKeyField(db_column='user_id', null=False, rel_model=User, to_field='id')
    lesson_id = ForeignKeyField(db_column='lesson_id', null=False, rel_model=Lesson, to_field='id')
    date = IntegerField(null=False)
    class Meta:
        database = main.jdb
        db_table = 'user_lesson'
```