[英]Update a record in a table in SQLAlchemy and Python
I have some problems when I try to update information in some tables.当我尝试更新某些表中的信息时,我遇到了一些问题。 For example, 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")
And I have this code:我有这个代码:
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)
When I do "rChannel.items.append(item)", I got this error:当我执行“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"
However, this instruction is working "rChannel.runtime = channel.runtime".但是,此指令正在运行“rChannel.runtime = channel.runtime”。
Any ideas?有任何想法吗?
Thanks in advance提前致谢
I think your code should be either:我认为您的代码应该是:
for chan in channels:
if rChannel.id == channel.id:
runtime = channel.runtime
or或者
for chan in channels:
if rChannel.id == channel.id:
for item in channel.items:
if item.id == 0:
rChannel.items.append(item)
But not both.但不是两者兼而有之。 It appears to me that you're adding
channel.items
twice to rChannel.items
.在我看来,您将
channel.items
添加到rChannel.items
两次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.