Skip to content
This repository has been archived by the owner on Aug 4, 2022. It is now read-only.

Commit

Permalink
Fixing a bug with querying non-id primary keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Arcangeli committed Nov 14, 2013
1 parent 5190465 commit 628e6af
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion stdnet/odm/query.py
Expand Up @@ -69,7 +69,7 @@ def __init__(self, meta, session, select_related=None,
'ordering': ordering,
'fields': fields,
'get_field': get_field}
self.name = name if name is not None else self.name
self.name = name if name is not None else meta.pk.name
self.keyword = keyword if keyword is not None else self.keyword

@property
Expand Down
20 changes: 19 additions & 1 deletion tests/all/fields/pk.py
Expand Up @@ -23,4 +23,22 @@ def test_add_parent_and_child(self):
self.assertEqual(child.parent_id, parent.pkvalue())
t.add(parent)
t.add(child)
yield t.on_result
yield t.on_result


class TestQuery(test.TestCase):
models = (Parent, Child)

def test_non_id_pk(self):
''' Models with non-'id' primary keys should be queryable '''
models = self.mapper
with models.session().begin() as t:
parent = models.parent(name='test2')
child = models.child(parent=parent, name='foo')
t.add(parent)
t.add(child)
yield t.on_result
with models.session().begin() as t:
parents = t.query(Parent).all()
self.assertEqual(len(parents), 1)
yield t.on_result

0 comments on commit 628e6af

Please sign in to comment.