jfinkels / flask-restless

NO LONGER MAINTAINED - A Flask extension for creating simple ReSTful JSON APIs from SQLAlchemy models.
https://flask-restless.readthedocs.io
GNU Affero General Public License v3.0
1.02k stars 301 forks source link

Hybrid_property crash #665

Open mixmastamyk opened 6 years ago

mixmastamyk commented 6 years ago

Hi, do you have any insight on this error I'm getting with SQLAlchemy and flask-restless? It is crashing with a 500 server error when I try to access /players which references a player group:

AttributeError: Neither 'hybrid_property' object nor 'ExprComparator' object associated with
Model.count has an attribute 'property'

Here are my models, so far:

class Players(db.Model):
    group_id = Column(Integer, ForeignKey('player_groups.id'), nullable=True)
    group = db.relationship('PlayerGroups', back_populates='members')

class PlayerGroups(db.Model):
    members = db.relationship('Players', back_populates='group')

    @hybrid_property
    def count(self):
        return len(self.members)

    @count.expression
    def count(cls):
        return db.select([db.func.count(Players.id)]).where(
                            Players.group_id == cls.id).label('count')

Thanks,

adwylie commented 6 years ago

Hey, I seem to be having this issue too, though just for version 0.17. I downgraded to 0.16 as a solution. A patch to the library also seemed to fix the issue, though I didn't check if the regression was solved in 1.0.0b1.

As far as the patch, I added a line in the method primary_key_names in helpers.py (as shown below, highlighted line). The method appears to be updated in the repo however, so odds are the issue has been corrected in future versions.

helpers_primary_key_names_updated

mixmastamyk commented 6 years ago

I'm using '1.0.0b1' btw.

eliransCyberhat commented 4 years ago

Since the issue was not implemented in any stable tag (nor in beta), my proposed workaround is to set the attribute called "property" on the hybrid_property, as follows and as mentioned in the stack overflow answer :

`

@hybrid_property
 def key_id(self):
     return int(self.key.split('-')[1])

@key_id.expression
def key_id(cls):
    return cast(func.split_part(cls.key, '-', 2), Integer)

key_id.__setattr__("property", "None")

`

mrevutskyi commented 4 years ago

Hi, there is a fork project Flask-Restless-NG that has this issue and some other issues fixed, along with performance improvements