Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If I understand the basic premise of the question (admittedly it's a little unclear), it sounds using <a href="http://www.sqlalchemy.org/docs/orm/relationships.html" rel="nofollow">relationships</a> might accomplish this better. From what I'm gathering, a picture has one album, but an album has many pictures? Anyway, that's somewhat irrelevant as the relationship concept is what's important here.</p> <p>You haven't specified how your model is setup, but using <a href="http://www.sqlalchemy.org/docs/orm/extensions/declarative.html" rel="nofollow">declarative</a> syntax you could define the tables like below:</p> <pre><code>class Album(Base): __tablename__ = 'albums' id = Column(Integer, Sequence('album_id_sequence'), primary_key=True) class Picture(Base): __tablename__ = 'pictures' id = Column(Integer, Sequence('picture_id_sequence'), primary_key=True) albumid = Column(Integer, ForeignKey('albums.id')) picture = Column(String(99)) album = relationship('Album', backref=backref('pictures')) </code></pre> <p>Sqlalchemy then takes care of all the hard work for you. Now every 'picture' has an 'album' property, callable via 'picture.album'. In code this looks like:</p> <pre><code>pictures = session.query(Picture).all() for picture in pictures: for album in picture.albums: # do stuff with album </code></pre> <p>And vice-versa, if you have an album, you can get a picture (this is what the backref in the relationship definition does):</p> <pre><code>albums = session.query(Album).all() for album in albums: album.picture # the picture object related to the album </code></pre> <p>I haven't tested the code so chances are there are typos, but hopefully you get the general idea.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload