Skip to content

Commit

Permalink
Fix for reflection when no primary key present.
Browse files Browse the repository at this point in the history
  • Loading branch information
coleifer committed Apr 9, 2023
1 parent d27a772 commit 44fafd1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
6 changes: 5 additions & 1 deletion playhouse/reflection.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,11 @@ class Meta:
# Fix models with multi-column primary keys.
composite_key = False
if len(primary_keys) == 0:
primary_keys = columns.keys()
if 'id' not in columns:
Meta.primary_key = False
else:
primary_keys = columns.keys()

if len(primary_keys) > 1:
Meta.primary_key = CompositeKey(*[
field.name for col, field in columns.items()
Expand Down
16 changes: 16 additions & 0 deletions tests/reflection.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ class Nugget(TestModel):
category = CharField()


class NoPK(TestModel):
data = CharField()
class Meta:
primary_key = False


class BaseReflectionTestCase(ModelTestCase):
def setUp(self):
super(BaseReflectionTestCase, self).setUp()
Expand Down Expand Up @@ -444,6 +450,16 @@ def test_get_field(self):
'%s not in %s' % (actual, fields))


class TestReflectNoPK(BaseReflectionTestCase):
requires = [NoPK]

def test_no_pk(self):
models = self.introspector.generate_models()
NoPK = models['no_pk']
self.assertEqual(NoPK._meta.sorted_field_names, ['data'])
self.assertTrue(NoPK._meta.primary_key is False)


class EventLog(TestModel):
data = CharField(constraints=[SQL('DEFAULT \'\'')])
timestamp = DateTimeField(constraints=[SQL('DEFAULT current_timestamp')])
Expand Down

0 comments on commit 44fafd1

Please sign in to comment.