Repository: pynamodb/PynamoDB

Issue:
Support for inheritance between models
Right now it is not possible to have a model `Foo` inherit from model `Bar`, i.e.:

```python
class Bar(Model):
    class Meta:
        table_name = 'bar_table'
...

class Foo(Bar):
    class Meta:
        table_name = 'foo_table'
...
```

Apparently the connection to the underlying tables is cached between these two models, which causes accesses to `Foo` to refer to the table 'bar_table' instead of 'foo_table'.
 

Discussion:

Patch:
diff --git a/pynamodb/connection/table.py b/pynamodb/connection/table.py
index eebd153..ddad6c3 100644
--- a/pynamodb/connection/table.py
+++ b/pynamodb/connection/table.py
@@ -31,8 +31,6 @@ class TableConnection:
         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,
diff --git a/pynamodb/models.py b/pynamodb/models.py
index 15944e3..17a62c8 100644
--- a/pynamodb/models.py
+++ b/pynamodb/models.py
@@ -1009,7 +1009,9 @@ class Model(AttributeContainer, metaclass=MetaModel):
                     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,


Test patch:
diff --git a/tests/test_model.py b/tests/test_model.py
index 71b8461..680ebd5 100644
--- a/tests/test_model.py
+++ b/tests/test_model.py
@@ -3256,3 +3256,18 @@ class ModelInitTestCase(TestCase):
         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)