Issue: Help:  AttributeError: when select with more than one join
Author: eguzmanf
State: closed
Created: 2018-05-17 17:06:16

**Hi Friends, 
I needs help please, I'm getting the following error when try a select with join:**

(work) C:\Users\eguzman\Documents\python-work\asterisk-insert-reminder>python script.py
Traceback (most recent call last):
  File "script.py", line 41, in <module>
    row.ageagendas.CODIGO, row.ageagendas.PER_ID, row.stkpersonas.DOCUMENTO))
AttributeError: 'AgeCitas' object has no attribute 'stkpersonas'

The code: **script.py**
**-----------------------------------------------------------------------------------------------------**
```python
import peewee as pw
from classes.model import MySqlDbModel, AgeCitas, AgeAgendas, StkPersonas


class SelectCitasToRemind(MySqlDbModel):

    @staticmethod
    def selectCitasToRemind():
        cit = AgeCitas.alias()
        age = AgeAgendas.alias()
        per = StkPersonas.alias()
        query = (cit.select(
            cit.CIT_ID, cit.AGE_ID, cit.FECHAHORA,
            age.AGE_ID, age.CODIGO, age.CEN_ID, age.PER_ID,
            per.PER_ID, per.DOCUMENTO, per.NOMBRE)
            .join(age, pw.JOIN.LEFT_OUTER, on=(cit.AGE_ID == age.AGE_ID))
            .join(per, pw.JOIN.LEFT_OUTER, on=(age.PER_ID == per.PER_ID))
            .where((cit.CIT_ID.is_null(False))
                   & (cit.PAC_ID.is_null(False))
                   )
            .limit(10)
        )
        return query


query = SelectCitasToRemind.selectCitasToRemind()

for row in query:
    print("{} | {} | {} | {} | {} | {}".format(
        row.CIT_ID, row.AGE_ID, row.FECHAHORA,
        row.ageagendas.CODIGO, row.ageagendas.PER_ID, row.stkpersonas.DOCUMENTO))
```

The code: **classes/model.py**
**-----------------------------------------------------------------------------------------------------**
```python
import peewee as pw


# Conection to mydbname database
dbn = 'mydbname'
user = 'myusername'
pwd = '******'
host = 'localhost'
prt = 3306

db = pw.MySQLDatabase(dbn, user=user,
                      password=pwd,  host=host, port=prt)


class MySqlDbModel(pw.Model):
    class Meta:
        database = db


class AgeCitas(MySqlDbModel):
    CIT_ID = pw.BigIntegerField()
    AGE_ID = pw.BigIntegerField()
    FECHAHORA = pw.DateTimeField()
    ACT_ID = pw.BigIntegerField()
    PAC_ID = pw.BigIntegerField()
    ASIGNADA = pw.CharField()
    IND_ID = pw.BigIntegerField()
    ACUDIO = pw.DoubleField()
    ORDEN = pw.DoubleField()

    class Meta:
        table_name = 'AGE_CITAS'


class AgeAgendas(MySqlDbModel):
    AGE_ID = pw.DecimalField()
    CODIGO = pw.CharField()
    CEN_ID = pw.DecimalField()
    PER_ID = pw.DecimalField()
    NPR_ID = pw.DecimalField()
    DESCRIPCION = pw.CharField()
    COLOR = pw.CharField()

    class Meta:
        table_name = 'AGE_AGENDAS'


class StkPersonas(MySqlDbModel):
    PER_ID = pw.BigIntegerField()
    DOCUMENTO = pw.CharField()
    NOMBRE = pw.CharField()
    APELLIDO1 = pw.CharField()
    APELLIDO2 = pw.CharField()
    FECHA_ALTA = pw.DateTimeField()
    FECHA_BAJA = pw.DateTimeField()
    EMAIL = pw.CharField()
    TELEFONO1 = pw.CharField()
    TELEFONO2 = pw.CharField()
    TELEFONO3 = pw.CharField()
    TELEFONO4 = pw.CharField()
    SEXO = pw.CharField()
    NACIMIENTO = pw.DateTimeField()
    ES_PACIENTE = pw.CharField()
    ES_PROFESIONAL = pw.CharField()

    class Meta:
        table_name = 'STK_PERSONAS'

```

Since you don't appear to have actual foreign-keys, you'll need to tell peewee how to reconstruct the model graph by providing aliases in your join predicates:

```python
           .join(age, pw.JOIN.LEFT_OUTER, on=(cit.AGE_ID == age.AGE_ID).alias('agecitas'))
           .join(per, pw.JOIN.LEFT_OUTER, on=(age.PER_ID == per.PER_ID).alias('sktpersona'))
```

Also one look at the tests should have revealed that this logic is tested thoroughly in peewee. Meaning you have a bug in your code rather than a bug in peewee.

For that you need to use StackOverflow or you can try `#peewee` on freenode IRC. This is not a troubleshooting support site.

Ok Thanks, I will ask on StackOverflow

For what it's worth, I *did* answer your question.