coleifer / peewee

a small, expressive orm -- supports postgresql, mysql, sqlite and cockroachdb
http://docs.peewee-orm.com/
MIT License
11.06k stars 1.37k forks source link

Inherited hybrid properties not using ModelAlias #2888

Closed mikemill closed 4 months ago

mikemill commented 4 months ago

peewee 3.17.3

This is similar to the issue from 2019 #1969. We are using a base class to define a couple of fields and related hybrid properties so that we can reuse it across multiple models. We've found that if we alias the model then the hybrid properties (and hybrid methods) don't use the alias and instead use the base class.

Example:

from peewee import Model, IntegerField
from playhouse.hybrid import hybrid_property

class Mixin:
    @hybrid_property
    def length(self):
        return self.end - self.start

class Interval(Model, Mixin):
    start = IntegerField()
    end = IntegerField()

Interval2 = Interval.alias()

query = Interval2.select(Interval2.length)

print(query)

The resulting query is SELECT ("t1"."end" - "t1"."start") FROM "interval" AS "t2"

I've debugged it down to ModelAlias.__getattr__ and obj = self.model.__dict__[attr]. My understanding is that inherited properties are not in the class' __dict__ so that code will throw the KeyError.

Is there a way to address this?

Thanks!