Skip to content

Commit

Permalink
Preserve column name when using unresolved deferred foreign-key.
Browse files Browse the repository at this point in the history
Fixes #1886
  • Loading branch information
coleifer committed Mar 15, 2019
1 parent ea12dad commit 091b781
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
4 changes: 3 additions & 1 deletion peewee.py
Original file line number Diff line number Diff line change
Expand Up @@ -4809,7 +4809,9 @@ def __init__(self, rel_model_name, **kwargs):
self.field_kwargs = kwargs
self.rel_model_name = rel_model_name.lower()
DeferredForeignKey._unresolved.add(self)
super(DeferredForeignKey, self).__init__()
super(DeferredForeignKey, self).__init__(
column_name=kwargs.get('column_name'),
null=kwargs.get('null'))

__hash__ = object.__hash__

Expand Down
16 changes: 16 additions & 0 deletions tests/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,14 @@ def test_deferred_foreign_key(self):


class TestDeferredForeignKeyResolution(ModelTestCase):
def test_unresolved_deferred_fk(self):
class Photo(Model):
album = DeferredForeignKey('Album', column_name='id_album')
class Meta:
database = get_in_memory_db()
self.assertSQL(Photo.select(), (
'SELECT "t1"."id", "t1"."id_album" FROM "photo" AS "t1"'), [])

def test_deferred_foreign_key_resolution(self):
class Base(Model):
class Meta:
Expand Down Expand Up @@ -416,6 +424,14 @@ class Album(Base):
'SELECT "t1"."id", "t1"."id_album", "t1"."id_Alt_album" '
'FROM "photo" AS "t1"'), [])

a = Album(id=3, alt_id=4)
self.assertSQL(a.pictures, (
'SELECT "t1"."id", "t1"."id_album", "t1"."id_Alt_album" '
'FROM "photo" AS "t1" WHERE ("t1"."id_album" = ?)'), [3])
self.assertSQL(a.alt_pix, (
'SELECT "t1"."id", "t1"."id_album", "t1"."id_Alt_album" '
'FROM "photo" AS "t1" WHERE ("t1"."id_Alt_album" = ?)'), [4])


class Composite(TestModel):
first = CharField()
Expand Down

0 comments on commit 091b781

Please sign in to comment.