Skip to content

Commit 5c8862e

Browse files
authored
Partial support for model inheritence backed by different tables. Fixes #361 (#863)
1 parent d2be48c commit 5c8862e

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

pynamodb/connection/table.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ def __init__(
3131
aws_secret_access_key: Optional[str] = None,
3232
aws_session_token: Optional[str] = None,
3333
) -> None:
34-
self._hash_keyname = None
35-
self._range_keyname = None
3634
self.table_name = table_name
3735
self.connection = Connection(region=region,
3836
host=host,

pynamodb/models.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,9 @@ def _get_connection(cls) -> TableConnection:
10091009
cls.__module__, cls.__name__,
10101010
),
10111011
)
1012-
if cls._connection is None:
1012+
# For now we just check that the connection exists and (in the case of model inheritance)
1013+
# points to the same table. In the future we should update the connection if any of the attributes differ.
1014+
if cls._connection is None or cls._connection.table_name != cls.Meta.table_name:
10131015
cls._connection = TableConnection(cls.Meta.table_name,
10141016
region=cls.Meta.region,
10151017
host=cls.Meta.host,

tests/test_model.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3256,3 +3256,18 @@ class Meta:
32563256
class ChildModel(ParentModel):
32573257
pass
32583258
self.assertEqual(ParentModel.Meta.table_name, ChildModel.Meta.table_name)
3259+
3260+
def test_connection_inheritance(self):
3261+
class Foo(Model):
3262+
class Meta:
3263+
table_name = 'foo'
3264+
class Bar(Foo):
3265+
class Meta:
3266+
table_name = 'bar'
3267+
class Baz(Foo):
3268+
pass
3269+
assert Foo._get_connection() is not Bar._get_connection()
3270+
assert Foo._get_connection() is Baz._get_connection()
3271+
self.assertEqual(Foo._get_connection().table_name, Foo.Meta.table_name)
3272+
self.assertEqual(Bar._get_connection().table_name, Bar.Meta.table_name)
3273+
self.assertEqual(Baz._get_connection().table_name, Baz.Meta.table_name)

0 commit comments

Comments
 (0)