Skip to content

Commit 914f4fc

Browse files
committed
Don't hook dunder methods on Entity.
This allows Entity to be deepcopied, which broke named-constraint inheritance. Fixes #3067
1 parent 7edbd32 commit 914f4fc

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

peewee.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,6 +1648,8 @@ def __init__(self, *path):
16481648
self._path = [p for p in path if p]
16491649

16501650
def __getattr__(self, attr):
1651+
if attr.startswith('__') and attr.endswith('__'):
1652+
return super(Entity, self).__getattr__(attr)
16511653
return Entity(*self._path + [attr])
16521654

16531655
def get_sort_key(self, ctx):

tests/schema.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,21 @@ class SmInt(Base):
912912
'"value" INTEGER NOT NULL, '
913913
'"label" VARCHAR(255) NOT NULL)'), [])
914914

915+
def test_constraint_inheritance(self):
916+
class Product(TestModel):
917+
price = IntegerField(constraints=[Check('price > 0', name='pc')])
918+
class Child(Product):
919+
pass
920+
921+
self.assertSQL(Product._schema._create_table(False), (
922+
'CREATE TABLE "product" ('
923+
'"id" INTEGER NOT NULL PRIMARY KEY, '
924+
'"price" INTEGER NOT NULL CONSTRAINT "pc" CHECK (price > 0))'), [])
925+
self.assertSQL(Child._schema._create_table(False), (
926+
'CREATE TABLE "child" ('
927+
'"id" INTEGER NOT NULL PRIMARY KEY, '
928+
'"price" INTEGER NOT NULL CONSTRAINT "pc" CHECK (price > 0))'), [])
929+
915930

916931
class TestDDLAdditionalSQL(ModelDatabaseTestCase):
917932
database = get_in_memory_db()

0 commit comments

Comments
 (0)