How to store and search list in SQLAlchemy?
--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Puzzle3
--
Chapters
00:00 How To Store And Search List In Sqlalchemy?
01:04 Accepted Answer Score 2
01:46 Thank you
--
Full question
https://stackoverflow.com/questions/1231...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #sqlalchemy #pyramid
#avk47
    Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Puzzle3
--
Chapters
00:00 How To Store And Search List In Sqlalchemy?
01:04 Accepted Answer Score 2
01:46 Thank you
--
Full question
https://stackoverflow.com/questions/1231...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #sqlalchemy #pyramid
#avk47
ACCEPTED ANSWER
Score 2
If you add backref onto your ItemAttribute relationship:
item_id = Column(Integer, ForeignKey('items.id', onupdate='CASCADE', ondelete='CASCADE'))
item = relationship(Items, backref='attributes')
This will create and Item.attributes[] array which contains the ItemAttribute's. You might also add the onupdate and ondelete if you're using mysql.
Then when you query, you can do this:
rs = mySession.query(Items)
firstItem = rs.first()
for attribute in firstItem.attributes:
   print attribute
When querying you can filter by joining the backref:
rs = mySession.query(Items).join(Items.attributes).filter(ItemAttribute.name=='somethingSpecial')
Further, if it's a one to one relationship (but it's not in this case), you could skip the list by specifing uselist=False:
item = relationship(ITEM, backref='attribute', uselist=False)