[sqlalchemy] Re: Updating a detached object
Channel is just type of object. I realize what my problem is. I don't know why my object isn't saved correctly. for chan in channels: if chan.id == channel.id: chan = session.merge(channel) break On Sep 13, 2:47 pm, Michael Bayer mike...@zzzcomputing.com wrote: On Sep 13, 2010, at 2:31 PM, Alvaro Reinoso wrote: Yes, I've done that. I doesn't work either. for chan in channels: if chan.id == channel.id: chan = session.merge(channel) break i dont understand the context of that code (what's channel) ? This is how a basic merge works: def merge_new_data(some_xml): my_objects = parse_xml(some_xml) # at this point, every object in my_objects should # have a primary key, as well as every child of every element, # all the way down. Existing primary keys must be fully populated, # that's your job. This is the intricate part, obviously. But you don't # merge anything here, just get the PKs filled in. # then you merge the whole thing. merge() cascades along all # relationships. The rule is simple - if PK is present and exists in the DB, it # updates. otherwise, it inserts. for obj in my_objects: Session.merge(obj) Session.commit() # done On Sep 13, 2:27 pm, Michael Bayer mike...@zzzcomputing.com wrote: On Sep 13, 2010, at 2:13 PM, Alvaro Reinoso wrote: 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? here: session.merge(channel) use the return value of merge(): channel = session.merge(channel) the returned channel plus all children is the fully merged result. Thanks! On Sep 10, 5:09 pm, Michael Bayer mike...@zzzcomputing.com 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:
[sqlalchemy] Re: Updating a detached object
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 mike...@zzzcomputing.com 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 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: Updating a detached object
On Sep 13, 2010, at 2:13 PM, Alvaro Reinoso wrote: 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? here: session.merge(channel) use the return value of merge(): channel = session.merge(channel) the returned channel plus all children is the fully merged result. Thanks! On Sep 10, 5:09 pm, Michael Bayer mike...@zzzcomputing.com 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 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: Updating a detached object
Yes, I've done that. I doesn't work either. for chan in channels: if chan.id == channel.id: chan = session.merge(channel) break On Sep 13, 2:27 pm, Michael Bayer mike...@zzzcomputing.com wrote: On Sep 13, 2010, at 2:13 PM, Alvaro Reinoso wrote: 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? here: session.merge(channel) use the return value of merge(): channel = session.merge(channel) the returned channel plus all children is the fully merged result. Thanks! On Sep 10, 5:09 pm, Michael Bayer mike...@zzzcomputing.com 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 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: Updating a detached object
On Sep 13, 2010, at 2:31 PM, Alvaro Reinoso wrote: Yes, I've done that. I doesn't work either. for chan in channels: if chan.id == channel.id: chan = session.merge(channel) break i dont understand the context of that code (what's channel) ? This is how a basic merge works: def merge_new_data(some_xml): my_objects = parse_xml(some_xml) # at this point, every object in my_objects should # have a primary key, as well as every child of every element, # all the way down. Existing primary keys must be fully populated, # that's your job. This is the intricate part, obviously. But you don't # merge anything here, just get the PKs filled in. # then you merge the whole thing. merge() cascades along all # relationships. The rule is simple - if PK is present and exists in the DB, it # updates. otherwise, it inserts. for obj in my_objects: Session.merge(obj) Session.commit() # done On Sep 13, 2:27 pm, Michael Bayer mike...@zzzcomputing.com wrote: On Sep 13, 2010, at 2:13 PM, Alvaro Reinoso wrote: 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? here: session.merge(channel) use the return value of merge(): channel = session.merge(channel) the returned channel plus all children is the fully merged result. Thanks! On Sep 10, 5:09 pm, Michael Bayer mike...@zzzcomputing.com 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