Issue: Model field name and the same name for subquery column
Author: ak4nv
State: closed
Created: 2018-10-26 14:26:02

Hi there!
Three (simplifed) models:
```python
class Tax(db.Model):
    id = pw.AutoField(db_column='tax_id')

class TaxName(db.Model):
    tax = pw.ForeignKeyField(Tax)
    name = pw.CharField(400, null=True)
    name_class = pw.CharField(100, null=True)

class Host(db.Model):
    id = IDField()
    name = pw.CharField(100)
    genus = pw.ForeignKeyField(Tax)
```

This code works fine:
```python
    qs = (Host
          .select(Host.id, TaxName.name.alias('genus'))
          .join(TaxName, on=((Host.genus == TaxName.tax) &
                             (TaxName.name_class == 'scientific name'))))
```
But if I make a subquery:
```python
    subq = (TaxName
            .select(TaxName.tax.alias('id'), TaxName.name)
            .where(TaxName.name_class == 'scientific name')).alias('subq')
    qs = (Host
          .select(Host.id, subq.c.name.alias('genus'))
          .join(subq, on=(Host.genus == subq.c.id)))
```
I get an error:
```
  File "/home/ak04nv/.virtualenvs/phd/lib/python3.7/site-packages/peewee.py", line 1706, in __len__
    return len(self._cursor_wrapper)
  File "/home/ak04nv/.virtualenvs/phd/lib/python3.7/site-packages/peewee.py", line 3696, in __len__
    self.fill_cache()
  File "/home/ak04nv/.virtualenvs/phd/lib/python3.7/site-packages/peewee.py", line 3737, in fill_cache
    iterator.next()
  File "/home/ak04nv/.virtualenvs/phd/lib/python3.7/site-packages/peewee.py", line 3793, in next
    self.cursor_wrapper.iterate()
  File "/home/ak04nv/.virtualenvs/phd/lib/python3.7/site-packages/peewee.py", line 3712, in iterate
    result = self.process_row(row)
  File "/home/ak04nv/.virtualenvs/phd/lib/python3.7/site-packages/peewee.py", line 6453, in process_row
    result[attr] = converters[i](row[i])
  File "/home/ak04nv/.virtualenvs/phd/lib/python3.7/site-packages/peewee.py", line 4497, in python_value
    return self.rel_field.python_value(value)
  File "/home/ak04nv/.virtualenvs/phd/lib/python3.7/site-packages/peewee.py", line 3948, in python_value
    return value if value is None else self.adapt(value)
ValueError: invalid literal for int() with base 10: 'Coarctotermes'
```
It seems `peewee` trying to convert `genus` field to an integer. How to fix it?
Any pieces of advice are helpful!
Thanks!

So peewee sees that you're aliasing a column value to an attribute that is a field on the given model (e.g., you're aliasing a string to the name "genus"), which happens to be a foreign-key. Peewee assumes that the value you're storing in the foreign-key field attribute is of the same type as the field would usually store -- an integer ID.

To work around this, just use a different alias that doesn't conflict with a field name.

```python
    qs = (Host
          .select(Host.id, TaxName.name.alias('genus_name'))
          .join(TaxName, on=((Host.genus == TaxName.tax) &
                             (TaxName.name_class == 'scientific name'))))

    # or
    subq = (TaxName
            .select(TaxName.tax.alias('id'), TaxName.name)
            .where(TaxName.name_class == 'scientific name')).alias('subq')
    qs = (Host
          .select(Host.id, subq.c.name.alias('genus_name'))
          .join(subq, on=(Host.genus == subq.c.id)))
```

That's less confusing, anyways, I would think, since your foreign-key attribute is not being overwritten with a string value.

@coleifer Yep, I tried to rename alias and it works fine. But I'd like to use the same name. So I used the first variant (without subquery). Thanks!