Repository: jazzband/django-simple-history

Issue:
Error when setting table_name with inherit=True
**Problem Statement**

When `table_name` is set with `inherit=True` in base model then error is thrown when `makemigrations` command is entered.

***For example:***

```
from django.db import models
from simple_history.models import HistoricalRecords

class BaseModel(models.Model):
    created_date = models.DateTimeField(auto_now_add=True)
    last_updated_date = models.DateTimeField(auto_now=True)
    history = HistoricalRecords(inherit=True, table_name='base_model_history')
  
class ChildModel(BaseModel):
    title = models.CharField(max_length=255, blank=False, null=False)

```    
***Following error is thrown with above code:***

(models.E028) db_table 'base_model_history' is used by multiple models: inherit.HistoricalBaseModel, inherit.HistoricalChildModel.


**Describe the solution you'd like**

***Cause of Problem***

As per my understanding, after looking at the source code, I found that `Meta.db_table` property of model is updated when `table_name` property is set. In case, when `inherit=True`, `self.table_name` still points to `table_name ` set in the base model when `create_history_model `method is invoked for the child model. This logic is implemented in line 301-302 in [create_history_model ](https://github.com/jazzband/django-simple-history/blob/master/simple_history/models.py) method.

***Possible Solution***

inherited flag is sent as an argument to `create_history_model` method, this can be used in the condition where table_name is checked. Condition at line 301 will look like this:
`if not inherited and self.table_name is not None:`

If it can not be done, then this behavior should be mentioned explicitly in the documentation.

**Describe alternatives you've considered**

Overridden `create_history_model` method to update `table_name` when inherited is set to True and table_name is set:

```
from django.db import models
from simple_history.models import HistoricalRecords


class CustomHistoricalRecords(HistoricalRecords):
    def create_history_model(self, model, inherited):
        if inherited and self.table_name:
            self.table_name=f"{model._meta.app_label}_{self.get_history_model_name(model)}"
        history_model = super().create_history_model(model, inherited)
        return history_model

class BaseModel(models.Model):
    created_date = models.DateTimeField(auto_now_add=True)
    last_updated_date = models.DateTimeField(auto_now=True)
    history = CustomHistoricalRecords(inherit=True, table_name="base_model_history")
  
class ChildModel(BaseModel):
    title = models.CharField(max_length=255, blank=False, null=False)
```

**Additional context**

I found this issue while looking for the solution of  #1174 



Discussion:

Resolving patch:
diff --git a/simple_history/models.py b/simple_history/models.py
--- a/simple_history/models.py
+++ b/simple_history/models.py
@@ -298,7 +298,7 @@ def create_history_model(self, model, inherited):
         attrs.update(self.get_extra_fields(model, fields))
         # type in python2 wants str as a first argument
         attrs.update(Meta=type("Meta", (), self.get_meta_options(model)))
-        if self.table_name is not None:
+        if not inherited and self.table_name is not None:
             attrs["Meta"].db_table = self.table_name
 
         # Set as the default then check for overrides


Test patch:
diff --git a/simple_history/tests/models.py b/simple_history/tests/models.py
--- a/simple_history/tests/models.py
+++ b/simple_history/tests/models.py
@@ -670,7 +670,7 @@ class InheritTracking4(TrackedAbstractBaseA):
 
 class BasePlace(models.Model):
     name = models.CharField(max_length=50)
-    history = HistoricalRecords(inherit=True)
+    history = HistoricalRecords(inherit=True, table_name="base_places_history")
 
 
 class InheritedRestaurant(BasePlace):
diff --git a/simple_history/tests/tests/test_models.py b/simple_history/tests/tests/test_models.py
--- a/simple_history/tests/tests/test_models.py
+++ b/simple_history/tests/tests/test_models.py
@@ -724,6 +724,24 @@ def test_history_diff_includes_changed_fields_of_base_model(self):
         self.assertEqual(delta.new_record, new_record)
         self.assertEqual(expected_change.field, delta.changes[0].field)
 
+    def test_history_table_name_is_not_inherited(self):
+        def assert_table_name(obj, expected_table_name):
+            history_model = obj.history.model
+            self.assertEqual(
+                history_model.__name__, f"Historical{obj._meta.model.__name__}"
+            )
+            self.assertEqual(history_model._meta.db_table, expected_table_name)
+
+        place = BasePlace.objects.create(name="Place Name")
+        # This is set in `BasePlace.history`
+        assert_table_name(place, "base_places_history")
+
+        r = InheritedRestaurant.objects.create(name="KFC", serves_hot_dogs=True)
+        self.assertTrue(isinstance(r, BasePlace))
+        # The default table name of the history model,
+        # instead of inheriting from `BasePlace`
+        assert_table_name(r, f"tests_Historical{r._meta.model.__name__}".lower())
+
     def test_history_diff_with_incorrect_type(self):
         p = Poll.objects.create(question="what's up?", pub_date=today)
         p.question = "what's up, man?"