当我尝试更新某些表中的信息时,我遇到了一些问题。例如,我有这个表:
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")
我有这段代码:
def insertXML(channels, strXml):
channel = Channel()
session = rdb.Session()
result = ""
channel.fromXML(strXml)
fillChannelTemplate(channel, channels)
rChannel = session.query(Channel).get(channel.id)
for chan in channels:
if rChannel.id == channel.id:
rChannel.runtime = channel.runtime
for item in channel.items:
if item.id == 0:
rChannel.items.append(item)
当我执行“rChannel.items.append(item)”时,我收到此错误:
"FlushError: New instance Channel at 0xaf6e48c with identity key
zeppelinlib.channel.ChannelTest.Channel , (152,) conflicts with
persistent instance Channel at 0xac2e8ac"
但是,此指令正在运行“rChannel.runtime = channel.runtime”。
有什么想法吗?
提前致谢
答案 0 :(得分:0)
我认为你的代码应该是:
for chan in channels:
if rChannel.id == channel.id:
runtime = channel.runtime
或
for chan in channels:
if rChannel.id == channel.id:
for item in channel.items:
if item.id == 0:
rChannel.items.append(item)
而不是两者兼而有之。 在我看来,您将channel.items添加两次到rChannel.items。