Skip to content

Commit

Permalink
Merge e73e139 into d2be48c
Browse files Browse the repository at this point in the history
  • Loading branch information
jpinner-lyft committed Oct 8, 2020
2 parents d2be48c + e73e139 commit 34bdc6a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
2 changes: 0 additions & 2 deletions pynamodb/connection/table.py
Expand Up @@ -31,8 +31,6 @@ def __init__(
aws_secret_access_key: Optional[str] = None,
aws_session_token: Optional[str] = None,
) -> None:
self._hash_keyname = None
self._range_keyname = None
self.table_name = table_name
self.connection = Connection(region=region,
host=host,
Expand Down
4 changes: 3 additions & 1 deletion pynamodb/models.py
Expand Up @@ -1009,7 +1009,9 @@ def _get_connection(cls) -> TableConnection:
cls.__module__, cls.__name__,
),
)
if cls._connection is None:
# For now we just check that the connection exists and (in the case of model inheritance)
# points to the same table. In the future we should update the connection if any of the attributes differ.
if cls._connection is None or cls._connection.table_name != cls.Meta.table_name:
cls._connection = TableConnection(cls.Meta.table_name,
region=cls.Meta.region,
host=cls.Meta.host,
Expand Down
15 changes: 15 additions & 0 deletions tests/test_model.py
Expand Up @@ -3256,3 +3256,18 @@ class Meta:
class ChildModel(ParentModel):
pass
self.assertEqual(ParentModel.Meta.table_name, ChildModel.Meta.table_name)

def test_connection_inheritance(self):
class Foo(Model):
class Meta:
table_name = 'foo'
class Bar(Foo):
class Meta:
table_name = 'bar'
class Baz(Foo):
pass
assert Foo._get_connection() is not Bar._get_connection()
assert Foo._get_connection() is Baz._get_connection()
self.assertEqual(Foo._get_connection().table_name, Foo.Meta.table_name)
self.assertEqual(Bar._get_connection().table_name, Bar.Meta.table_name)
self.assertEqual(Baz._get_connection().table_name, Baz.Meta.table_name)

0 comments on commit 34bdc6a

Please sign in to comment.