Issue: 'column_name' argument of DeferredForeignKey field is ignored
Author: droserasprout
State: closed
Created: 2019-03-14 12:51:27

```
class Task(Model):
    [...]
    CreateUser = DeferredForeignKey('User', column_name='createuser', null=True, field='ID', index=False)
    UpdateUser = DeferredForeignKey('User', column_name='updateuser', null=True, field='ID', index=False)

>>> Task.select().first()
2019-03-14 12:41:03,109 DEBUG    [peewee.py:2823] ('SELECT "t1"."id", "t1"."updatedt", "t1"."createdt", "t1"."state", "t1"."CreateUser", "t1"."UpdateUser", "t1"."name", "t1"."type", "t1"."start_time", "t1"."finish_time", "t1"."deadline", "t1"."result", "t1"."scenario", "t1"."wait_time", "t1"."cases_id", "t1"."locked_by", "t1"."description", "t1"."comments", "t1"."responsible_role", "t1"."previous_tasks" FROM "tasks" AS "t1" LIMIT %s', [1])
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/peewee.py", line 2835, in execute_sql
    cursor.execute(sql, params or ())
  File "/usr/local/lib/python3.6/site-packages/psycopg2/extras.py", line 141, in execute
    return super(DictCursor, self).execute(query, vars)
psycopg2.ProgrammingError: column t1.CreateUser does not exist
LINE 1: ..., "t1"."updatedt", "t1"."createdt", "t1"."state", "t1"."Crea...
                                                             ^
HINT:  Perhaps you meant to reference the column "t1.createuser".


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.6/site-packages/peewee.py", line 1698, in inner
    return method(self, database, *args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/peewee.py", line 1958, in first
    return self.peek(database, n=n)
  File "/usr/local/lib/python3.6/site-packages/peewee.py", line 1698, in inner
    return method(self, database, *args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/peewee.py", line 1949, in peek
    rows = self.execute(database)[:n]
  File "/usr/local/lib/python3.6/site-packages/peewee.py", line 1698, in inner
    return method(self, database, *args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/peewee.py", line 1769, in execute
    return self._execute(database)
  File "/usr/local/lib/python3.6/site-packages/peewee.py", line 1943, in _execute
    cursor = database.execute(self)
  File "/usr/local/lib/python3.6/site-packages/playhouse/postgres_ext.py", line 464, in execute
    cursor = self.execute_sql(sql, params, commit=commit)
  File "/usr/local/lib/python3.6/site-packages/peewee.py", line 2842, in execute_sql
    self.commit()
  File "/usr/local/lib/python3.6/site-packages/peewee.py", line 2627, in __exit__
    reraise(new_type, new_type(*exc_args), traceback)
  File "/usr/local/lib/python3.6/site-packages/peewee.py", line 178, in reraise
    raise value.with_traceback(tb)
  File "/usr/local/lib/python3.6/site-packages/peewee.py", line 2835, in execute_sql
    cursor.execute(sql, params or ())
  File "/usr/local/lib/python3.6/site-packages/psycopg2/extras.py", line 141, in execute
    return super(DictCursor, self).execute(query, vars)
peewee.ProgrammingError: column t1.CreateUser does not exist
LINE 1: ..., "t1"."updatedt", "t1"."createdt", "t1"."state", "t1"."Crea...
                                                             ^
HINT:  Perhaps you meant to reference the column "t1.createuser".
```

May be related to #1812

Thanks for the excellent ticket -- I'll take a look.

I cannot replicate this issue. I've added some tests which I would have thought would reveal the problem, but they are passing without issue: ea12dadf5f370e470e05c760c02be4e7dca1edcd

If you can comment / share a test-case that replicates the problem, I will reopen.

@coleifer thanks for your time. I can reproduce this issue in my notebook but it's messy now, will provide updated tests after some digging.

@NicolasCaous first, you wrong, database is initialized properly. Second, you're pretty rude, man. I have provided just enough data to get maintainer's answer that's not an expected behaviour in some way.

At the time of `Task.select` execution `CreateUser` field is not resolved yet. I'd expect `DeferredForeignKey` to act like `IntegerField` (*_id) in those situations. Or maybe some note should be added to the docs.

```
from playhouse import postgres_ext as pw
from playhouse.pool import PooledPostgresqlExtDatabase
from playhouse.postgres_ext import (ArrayField, AutoField, CharField,
                                    DateTimeField, DeferredForeignKey,
                                    ForeignKeyField, IntegerField)
from psycopg2.extras import DictCursor

pg_main = PooledPostgresqlExtDatabase(
    None,
    timeout=180,
    stale_timeout=900,
    max_connections=32,
    cursor_factory=DictCursor
)

pg_main.init(
    database='postgres',
    user='postgres',
    password=None,
    host='172.17.0.5',
    port=5432
)

class Task(pw.Model):
    ID = AutoField(column_name='id')
    UpdateDate = DateTimeField(column_name='updatedt', null=True)
    CreateDate = DateTimeField(column_name='createdt', null=True)
    State = IntegerField(column_name='state', default=0)
    CreateUser = DeferredForeignKey('User', column_name='createuser', null=True, field='ID', index=False)
    UpdateUser = DeferredForeignKey('User', column_name='updateuser', null=True, field='ID', index=False)
    
    class Meta:
        database = pg_main
        table_name = 'tasks'

# class User(pw.Model):
#     ID = AutoField(column_name='id')
#     UpdateDate = DateTimeField(column_name='updatedt', null=True)
#     CreateDate = DateTimeField(column_name='createdt', null=True)
#     State = IntegerField(column_name='state', default=0)
#     CreateUser = IntegerField(column_name='createuser', null=True, index=False)
#     UpdateUser = IntegerField(column_name='updateuser', null=True, index=False)
    
#     class Meta:
#         database = pg_main
#         table_name = 'users'
        
with pg_main:
#     User.drop_table()
#     User.create_table()
#     Task.drop_table()
#     Task.create_table()
    tsk = Task.select().first()
```

Yeah I removed his comments. I was not being sarcastic when I said this was an excellent ticket.

Ok I didn't think about the possibility that you would attempt a query before all the models were imported / eval'd. I'll take another look.

Honestly, I kinda feel like attempting to query with an unresolved deferred foreign-key should be an error. But for now, I've just decided to carry forward the column name, so that it works as you'd expect.