Re: [sqlalchemy] Updating a detached object

2010-09-10 Thread Michael Bayer

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  with identity key
> (, (152,)) conflicts
> with persistent instance http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] Updating a detached object

2010-09-10 Thread Alvaro Reinoso
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  with identity key
(, (152,)) conflicts
with persistent instance http://groups.google.com/group/sqlalchemy?hl=en.