Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hybrid_property crash #665

Open
mixmastamyk opened this issue Oct 14, 2017 · 4 comments
Open

Hybrid_property crash #665

mixmastamyk opened this issue Oct 14, 2017 · 4 comments

Comments

@mixmastamyk
Copy link

mixmastamyk commented Oct 14, 2017

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
Copy link

adwylie commented Oct 24, 2017

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
Copy link
Author

I'm using '1.0.0b1' btw.

mickael-ange referenced this issue in mickael-ange/flask-restless Dec 9, 2018
Workaround hybrid_property issue: AttributeError: Neither 'hybrid_property' object nor 'ExprComparator' object associated with MyModel.count has an attribute 'property'
@eliransCyberhat
Copy link

eliransCyberhat commented Mar 2, 2020

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants