Python 更新分离对象时出现问题
我有这张桌子:Python 更新分离对象时出现问题,python,sqlalchemy,Python,Sqlalchemy,我有这张桌子: 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"
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")
我有一个通道列表,但它们是分离的对象。我使用joinedload选项获取它们,因为我有时会计算这些对象。当我这样做时,我会更新对象
这一次,我试图向分离的通道对象添加一个新项。代码如下:
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)
该项插入到数据库中,但它不会在通道\u项中创建关系
此外,我还发现了以下错误:
FlushError: New instance <Channel at 0xb75eeec> with identity key
(<class 'zeppelinlib.channel.ChannelTest.Channel'>, (152,))
conflicts with persistent instance <Channel at 0xb598dec
FlushError:具有标识键的新实例
(, (152,))
与持久实例冲突这是重复的