Issue: Inherit database models
Author: cLupus
State: closed
Created: 2018-01-31 10:21:36

Hi,
In version 2.10.2, one could define a new model / table with fields and then create a new model that inherits from said model (though foreign keys was a hassle).

ex. 

```
class A(BaseModel):
    common = TextField()


class B(A):
    specific = TextField()
```

However, in 3.0.6, this does not work, and gives an error message where the recursion limit has been reached.

```
test_domain_model.py:None (test_domain_model.py)
test_domain_model.py:3: in <module>
    from src.models.domain import APSModel, ParameterNames
../../models/domain.py:654: in <module>
    class LinearTrend(Trend):
../../../venv/lib/python3.6/site-packages/peewee.py:4811: in __new__
    parent_pk = deepcopy(base_meta.primary_key)
../../../venv/lib/python3.6/copy.py:180: in deepcopy
    y = _reconstruct(x, memo, *rv)
../../../venv/lib/python3.6/copy.py:281: in _reconstruct
    if hasattr(y, '__setstate__'):
../../../venv/lib/python3.6/site-packages/peewee.py:4134: in __getattr__
    if attr in self.rel_model._meta.fields:
../../../venv/lib/python3.6/site-packages/peewee.py:4134: in __getattr__
    if attr in self.rel_model._meta.fields:
../../../venv/lib/python3.6/site-packages/peewee.py:4134: in __getattr__
    if attr in self.rel_model._meta.fields:
E   RecursionError: maximum recursion depth exceeded while calling a Python object
!!! Recursion detected (same locals & position)
```


Check out test-case I added in c80c9d4bc64327237b0a8c8de208c5f41e0bed40 -- I'm also developing using Python 3.6 and encounter no such error.

Are you trying to inherit a `ForeignKeyField`?

Looks like it -- I've added a test and a patch: a446a9f244d3b530b79b03ab97923d4081780c71

From a semantic point-of-view, inheriting foreign-keys is a bit weird.

In some of the cases, I want to inherit the foreign key, yes.
It would be nice not having to deal with it in properties, or other logic (i.e. look through the different `<tablename>_set` properties).

I tried without the foreign key reference, and it seem to have worked.

Also, thanks for quick patch, and response 😄 

I've added code to re-set the backref (`'<table>_set'`) so each inherited foreign-key instance will get its own backref on the related model.

In 2.10.2, when I did this, the model the different inherited models referred to had a few extra fields, e.g.
`basetable_set`, `tablea_set`, `tableb_set`, `tablec_set`, and `tabled_set`. Now however (3.0.7), there is only one; `basetable_set` (which refers to `tabled_set`).

What do you suggest as a solution to access all the references that would have been created in 2.10.2, @coleifer ?

I'm not sure I follow. In 8a6e080dd3b258e8fc547c6cdf47e8de75635d60 I added a change for clearing out the inherited FK backref so that it would be unique for each inherited model. See the test-cases and some assertions, specifically https://github.com/coleifer/peewee/blob/8a6e080dd3b258e8fc547c6cdf47e8de75635d60/tests/models.py#L1264-L1265

To put it another way:

```python

class Category(Model):
    name = TextField()

class BaseContent(Model):
    category = ForeignKeyField(Category)

class Photo(BaseContent):
    image = TextField()

class Note(BaseContent):
    content = TextField()

# Now, if I'm on Category, I can:
category = Category.get(Category.name == 'whatever')

# List notes associated w/the category:
for note in category.note_set:
    ...

# List images associated w/the category:
for photo in category.photo_set:
    ...
```

Am I misunderstanding your comment?

You are understanding my comment.

In my case, though, I only see the latter (e.i. note_set).
Also, the foreign key works as a primary key in my case (i.e. `ForeignKey(..., primary_key=True)`)

Cool, I think this should address the issue with foreign-keys as primary-key: 8670ddb7835491cb75676cf7d70d3bd1c281aefe