Issue: Left join doen't work 
Author: JPilarr
State: closed
Created: 2021-02-04 15:29:02


```
  query = (Job.select(Job, Printer, PrintFile)
           .where(*conds)
           .join(Printer)
           .join(PrintFile,
                 pw.JOIN.LEFT_OUTER,
                 on=PrintFile.hash == Job.hash)
           .order_by(-Job.start, Job.printer, -Job.id)
           .limit(limit)
           .offset(offset))
```

The select doen't select PrintFile return only Job and Printer. PrintFile.hash is pw.CharField(max_length=28) and pw.CharField(null=True).
Select is created ok. The data doesn't packed into an object.

SELECT `t1`.`id`, `t1`.`printer_id`, `t1`.`origin_id`, `t1`.`path`, `t1`.`hash`, `t1`.`user_id`, `t1`.`state`, `t1`.`start`, `t1`.`end`, `t1`.`info_requested`, `t2`.`id`, `t2`.`name`, `t2`.`firmware`, `t2`.`appendix`, `t2`.`created`, `t2`.`location`, `t2`.`sn`, `t2`.`uuid`, `t2`.`registration_code`, `t2`.`printer_type_id`, `t2`.`team_id`, `t2`.`deleted`, `t2`.`sn_status`, `t2`.`settings`, `t3`.`hash`, `t3`.`filename`, `t3`.`uploaded`, `t3`.`size`, `t3`.`team_id`, `t3`.`printer_type_id`, `t3`.`meta`, `t3`.`m_time`, `t3`.`preview`, `t3`.`preview_mimetype` FROM `job` AS `t1` INNER JOIN `printer` AS `t2` ON (`t1`.`printer_id` = `t2`.`id`) LEFT OUTER JOIN `print_file` AS `t3` ON (`t3`.`hash` = `t1`.`hash`) WHERE (`t1`.`printer_id` = 169) ORDER BY `t1`.`start` DESC, `t1`.`printer_id`, `t1`.`id` DESC LIMIT 20 OFFSET 0




How are you accessing `printfile`? You may need to explicitly specify the target attribute you want your joined `PrintFile` instance to be bound to:

```python

query = (Job.select(Job, Printer, PrintFile)
           .where(*conds)
           .join(Printer)
           .join(PrintFile,
                 pw.JOIN.LEFT_OUTER,
                 on=(PrintFile.hash == Job.hash),
                 attr='printfile')
```

See:

* http://docs.peewee-orm.com/en/latest/peewee/relationships.html#joining-on-arbitrary-fields
* http://docs.peewee-orm.com/en/latest/peewee/relationships.html#selecting-from-multiple-sources

I read the documentaion more carefull but still doesn't work..
The join should ceate attribute printfile in Job class like instance of PrintFile.
```
        for i in query:
            print(" field job meta>", i.printfile.meta, i.printfile.preview)
```
This should be work. I think but....

```
AttributeError("'Job' object has no attribute 'printfile'") 
```




The join context at that time is `Printer -> PrintFile`. You will want to write the following:

```python

query = (Job.select(Job, Printer, PrintFile)
           .where(*conds)
           .join(Printer)
           .join_from(Job, PrintFile,
                 pw.JOIN.LEFT_OUTER,
                 on=(PrintFile.hash == Job.hash),
                 attr='printfile')
```

Or alternatively:

```python
query = (Job.select(Job, Printer, PrintFile)
           .where(*conds)
           .join(Printer)
           .switch(Job)
           .join(PrintFile,
                 pw.JOIN.LEFT_OUTER,
                 on=(PrintFile.hash == Job.hash),
                 attr='printfile')
```

This is it... thank you for your cooperation.