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.

Reply via email to