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 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