Skip to content

Commit

Permalink
Fix for #674.
Browse files Browse the repository at this point in the history
  • Loading branch information
coleifer committed Jul 29, 2015
1 parent fd59fb7 commit c1030f5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
3 changes: 2 additions & 1 deletion peewee.py
Original file line number Diff line number Diff line change
Expand Up @@ -1543,7 +1543,8 @@ def _parse(self, node, alias_map, conv):
sql = '(%s)' % sql
elif isinstance(node, Model):
sql = self.interpolation
if conv and isinstance(conv, ForeignKeyField):
if conv and isinstance(conv, ForeignKeyField) and \
not isinstance(conv.to_field, ForeignKeyField):
params = [
conv.to_field.db_value(getattr(node, conv.to_field.name))]
else:
Expand Down
5 changes: 5 additions & 0 deletions playhouse/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ class JobExecutionRecord(TestModel):
status = CharField()


class JERRelated(TestModel):
jer = ForeignKeyField(JobExecutionRecord)


class TestModelA(TestModel):
field = CharField(primary_key=True)
data = CharField()
Expand Down Expand Up @@ -363,6 +367,7 @@ class EmptyModel(TestModel):
BlobModel,
Job,
JobExecutionRecord,
JERRelated,
TestModelA,
TestModelB,
TestModelC,
Expand Down
19 changes: 18 additions & 1 deletion playhouse/tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ def test_non_int_fk(self):


class TestPrimaryKeyIsForeignKey(ModelTestCase):
requires = [Job, JobExecutionRecord]
requires = [Job, JobExecutionRecord, JERRelated]

def test_primary_foreign_key(self):
# we have one job, unexecuted, and therefore no executed jobs
Expand All @@ -539,6 +539,23 @@ def test_primary_foreign_key(self):
self.assertRaises(Exception, JobExecutionRecord.create, job=job, status='success')
test_db.rollback()

def test_pk_fk_relations(self):
j1 = Job.create(name='j1')
j2 = Job.create(name='j2')
jer1 = JobExecutionRecord.create(job=j1, status='1')
jer2 = JobExecutionRecord.create(job=j2, status='2')
jerr1 = JERRelated.create(jer=jer1)
jerr2 = JERRelated.create(jer=jer2)

jerr_j1 = [x for x in jer1.jerrelated_set]
self.assertEqual(jerr_j1, [jerr1])

jerr_j2 = [x for x in jer2.jerrelated_set]
self.assertEqual(jerr_j2, [jerr2])

jerr1_db = JERRelated.get(JERRelated.jer == j1)
self.assertEqual(jerr1_db, jerr1)


class TestFieldDatabaseColumn(ModelTestCase):
requires = [DBUser, DBBlog]
Expand Down

0 comments on commit c1030f5

Please sign in to comment.