If I merge the updated channel like you can see in this piece of code
it's working:

        def insertXML(channels, strXml):
                """Insert a new channel given XML string"""
                channel = Channel()
                session = rdb.Session()

                channel.fromXML(strXml)
                fillChannelTemplate(channel, channels)
                for item in channel.items:
                                if item.id == 0:
                                        item.id = None
                                        break
                session.merge(channel)

                for chan in channels:
                        if chan.id == channel.id:
                                chan.items.append(item)
                                break

My problem is I'm using channels, it's a list of channels which I save
it in HTTP session object. The channels list is a detached object
which I get using joinload option. So in this case, I update the
object correctly in database, but It isn't persistent in channels if I
do this:

                        for chan in channels:
                                if chan.id == channel.id:
                                        chan.items.append(item)
                                        break

Do you have any idea how I can solve this problem? or another
approach?

Thanks!

On Sep 10, 5:09 pm, Michael Bayer <[email protected]> wrote:
> On Sep 10, 2010, at 2:57 PM, Alvaro Reinoso wrote:
>
>
>
> > Hello guys,
>
> > I have this table:
>
> >    class Channel(rdb.Model):
> >            rdb.metadata(metadata)
> >            rdb.tablename("channels")
>
> >            id = Column("id", Integer, primary_key=True)
> >            title = Column("title", String(100))
> >            hash = Column("hash", String(50))
> >            runtime = Column("runtime", Float)
>
> >            items = relationship(MediaItem, secondary="channel_items",
> > order_by=MediaItem.position, backref="channels")
>
> > I have a list of channels, but they are detached objects. I get them
> > using joinedload option because I maniputale those objects sometimes.
> > When I do that, I update the object.
>
> > This time, I'm trying to add a new item to a detached channel object.
> > This is the code:
>
> >    def insertXML(channels, strXml):
> >            """Insert a new channel given XML string"""
> >            channel = Channel()
> >            session = rdb.Session()
> >            result = ""
>
> >            channel.fromXML(strXml)
> >            fillChannelTemplate(channel, channels)
> >            if channel.id == 0:
> >                    session.add(channel)
> >                    session.flush()
> >                    channels.append(channel)
> >            else:
> >                    for chan in channels:
> >                            if chan.id == channel.id:
> >                                    chan.runtime = channel.runtime
> >                                    chan.modified = datetime.date.today()
>
> >                                    for item in channel.items:
> >                                            if item.id == 0:
> >                                                    chan.items.append(item)
>
> >                    session.merge(chan)
>
> > The item is inserted in the database, but It doesn't create the
> > relation in channel_items.
>
> > Besides, I get this error:
>
> > FlushError: New instance <Channel at 0xb75eeec> with identity key
> > (<class 'zeppelinlib.channel.ChannelTest.Channel'>, (152,)) conflicts
> > with persistent instance <Channel at 0xb598dec
>
> anytime you have that error you should be using merge() to merge state into 
> that which is already existing, the return value from merge() is then what 
> you need to use for your new state.   I see you tried using merge earlier but 
> your issue is not clear.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to