SQLAlchemy: Hybrid expression with relationship

后端 未结 1 1181
慢半拍i
慢半拍i 2020-12-29 19:17

I have two Flask-SQLAlchemy models with a simple one-to-many relationship, like the minimal example below:

class School(db.Model):
    id = db.Column(db.Inte         


        
相关标签:
1条回答
  • 2020-12-29 19:41

    A much simpler approach for a simple case like this is an association proxy:

    class Teacher(db.Model):
        school_name = associationproxy('school', 'name')
    

    This supports querying (at least with ==) automatically.

    I'm curious how the hybrid select() example didn't work for you, since that's the easiest way to fix this within a hybrid. And for the sake of completion, you could also use a transformer to amend the query directly rather than subquerying.

    0 讨论(0)
提交回复
热议问题