[sqlalchemy] Re: Problem with one relation
The relation is OK, one-to-many between Playlist and PlaylisItem. However, PlaylistItem can contain one Playlist or one Layout and that Playlist could be in many PlaylistItems. I know it's weird relation if I already have Playlist as parent of PlaylistItem, but could I get this? Thanks! On Dec 6, 3:51 pm, Michael Bayer mike...@zzzcomputing.com wrote: On Dec 6, 2010, at 2:39 PM, Alvaro Reinoso wrote: Hi all, I have those two classes: class Playlist(rdb.Model): Represents playlist. It may contain playlist items rdb.metadata(metadata) rdb.tablename(playlists) id = Column(id, Integer, primary_key=True) title = Column(title, String(50)) pending = Column(pending, Boolean) items = relationship(PlaylistItem, cascade=all, delete, backref=playlists) screens = relationship(Screen, secondary=playlist_screens, backref=playlists) class PlaylistItem(rdb.Model): Represents a playlist of items in Schedule page rdb.metadata(metadata) rdb.tablename(playlist_items) id = Column(id, Integer, primary_key=True) title = Column(title, String(50)) runtime = Column(runtime, Integer) layoutId = Column(layout_id, Integer, ForeignKey(layouts.id)) playlistId = Column(playlist_id, Integer, ForeignKey(playlists.id)) layout = relationship(Layout, uselist=False) playlist = relationship(Playlist, uselist=False) One playlist can contain many PlaylistItem and PlaylistItem could contain layout or another playlist. The problem is when adding a PlaylistItem to a Playlist, PlaylistItem automatically gets the id of its parent (playlist_id). How can I avoid this? Thanks in advance! the items collection of Playlist represents a one-to-many reference to a list of PlaylistItems. A PlaylistItem in turn can only be referenced by one parent. The items/playlists relationships therefore manage the playlistId attribute to be the one foreign key represented by this ownership. If a PlaylistItem is capable of having multiple Playlist parents, this relationship should be changed to a many-to-many. Reference on relationship() patterns is athttp://www.sqlalchemy.org/docs/orm/relationships.html#basic-relationa -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalch...@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group athttp://groups.google.com/group/sqlalchemy?hl=en. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalch...@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
Re: [sqlalchemy] Re: Problem with one relation
On Dec 7, 2010, at 10:09 AM, Alvaro Reinoso wrote: The relation is OK, one-to-many between Playlist and PlaylisItem. However, PlaylistItem can contain one Playlist or one Layout and that Playlist could be in many PlaylistItems. I know it's weird relation if I already have Playlist as parent of PlaylistItem, but could I get this? If you mean, you want PlaylistItem.playlist to be independent of Playlist.items, just use two different foreign keys and two different relationships. Otherwise I don't really have enough detail here to know what you're asking for. Thanks! On Dec 6, 3:51 pm, Michael Bayer mike...@zzzcomputing.com wrote: On Dec 6, 2010, at 2:39 PM, Alvaro Reinoso wrote: Hi all, I have those two classes: class Playlist(rdb.Model): Represents playlist. It may contain playlist items rdb.metadata(metadata) rdb.tablename(playlists) id = Column(id, Integer, primary_key=True) title = Column(title, String(50)) pending = Column(pending, Boolean) items = relationship(PlaylistItem, cascade=all, delete, backref=playlists) screens = relationship(Screen, secondary=playlist_screens, backref=playlists) class PlaylistItem(rdb.Model): Represents a playlist of items in Schedule page rdb.metadata(metadata) rdb.tablename(playlist_items) id = Column(id, Integer, primary_key=True) title = Column(title, String(50)) runtime = Column(runtime, Integer) layoutId = Column(layout_id, Integer, ForeignKey(layouts.id)) playlistId = Column(playlist_id, Integer, ForeignKey(playlists.id)) layout = relationship(Layout, uselist=False) playlist = relationship(Playlist, uselist=False) One playlist can contain many PlaylistItem and PlaylistItem could contain layout or another playlist. The problem is when adding a PlaylistItem to a Playlist, PlaylistItem automatically gets the id of its parent (playlist_id). How can I avoid this? Thanks in advance! the items collection of Playlist represents a one-to-many reference to a list of PlaylistItems. A PlaylistItem in turn can only be referenced by one parent. The items/playlists relationships therefore manage the playlistId attribute to be the one foreign key represented by this ownership.If a PlaylistItem is capable of having multiple Playlist parents, this relationship should be changed to a many-to-many.Reference on relationship() patterns is athttp://www.sqlalchemy.org/docs/orm/relationships.html#basic-relationa -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalch...@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group athttp://groups.google.com/group/sqlalchemy?hl=en. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalch...@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalch...@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
[sqlalchemy] Re: Problem with one relation
Yes, that's what I want. I already have the two relations that I want.: One is in Playlist, items = relationship(PlaylistItem, cascade=all, delete, backref=playlists). If I understand properly, this allows Playlist to contain PlaylistItems And the another one is in PlaylistItem, playlist = relationship(Playlist, uselist=False). This one is the one that allows the PlaylistItem to contain objects of Playlist type. (Or at least, that's what I'd like it to be). This shouldn't be pointing to its Playlist parent, but to a different Playlist (which this item contains, because a Playlist can contain other Playlists inside) I guess for that I'd need another ForeignKey(playlists.id), but I don't really know how to differentiate this ForeignKey from the one that is pointing to its parent Playlist object (or record). Thank you for your help, btw On Dec 7, 10:16 am, Michael Bayer mike...@zzzcomputing.com wrote: On Dec 7, 2010, at 10:09 AM, Alvaro Reinoso wrote: The relation is OK, one-to-many between Playlist and PlaylisItem. However, PlaylistItem can contain one Playlist or one Layout and that Playlist could be in many PlaylistItems. I know it's weird relation if I already have Playlist as parent of PlaylistItem, but could I get this? If you mean, you want PlaylistItem.playlist to be independent of Playlist.items, just use two different foreign keys and two different relationships. Otherwise I don't really have enough detail here to know what you're asking for. Thanks! On Dec 6, 3:51 pm, Michael Bayer mike...@zzzcomputing.com wrote: On Dec 6, 2010, at 2:39 PM, Alvaro Reinoso wrote: Hi all, I have those two classes: class Playlist(rdb.Model): Represents playlist. It may contain playlist items rdb.metadata(metadata) rdb.tablename(playlists) id = Column(id, Integer, primary_key=True) title = Column(title, String(50)) pending = Column(pending, Boolean) items = relationship(PlaylistItem, cascade=all, delete, backref=playlists) screens = relationship(Screen, secondary=playlist_screens, backref=playlists) class PlaylistItem(rdb.Model): Represents a playlist of items in Schedule page rdb.metadata(metadata) rdb.tablename(playlist_items) id = Column(id, Integer, primary_key=True) title = Column(title, String(50)) runtime = Column(runtime, Integer) layoutId = Column(layout_id, Integer, ForeignKey(layouts.id)) playlistId = Column(playlist_id, Integer, ForeignKey(playlists.id)) layout = relationship(Layout, uselist=False) playlist = relationship(Playlist, uselist=False) One playlist can contain many PlaylistItem and PlaylistItem could contain layout or another playlist. The problem is when adding a PlaylistItem to a Playlist, PlaylistItem automatically gets the id of its parent (playlist_id). How can I avoid this? Thanks in advance! the items collection of Playlist represents a one-to-many reference to a list of PlaylistItems. A PlaylistItem in turn can only be referenced by one parent. The items/playlists relationships therefore manage the playlistId attribute to be the one foreign key represented by this ownership. If a PlaylistItem is capable of having multiple Playlist parents, this relationship should be changed to a many-to-many. Reference on relationship() patterns is athttp://www.sqlalchemy.org/docs/orm/relationships.html#basic-relationa -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalch...@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group athttp://groups.google.com/group/sqlalchemy?hl=en. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalch...@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group athttp://groups.google.com/group/sqlalchemy?hl=en. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalch...@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.