gpt4 book ai didi

python - 将对象添加到 SQLAlchemy 关联对象时出现 KeyError

转载 作者:太空狗 更新时间:2023-10-29 20:16:04 26 4
gpt4 key购买 nike

我有两个表,tabletcorrespondent:

class Correspondent(db.Model, GlyphMixin):
# PK column and tablename etc. come from the mixin
name = db.Column(db.String(100), nullable=False, unique=True)
# association proxy
tablets = association_proxy('correspondent_tablets', 'tablet')

def __init__(self, name, tablets=None):
self.name = name
if tablets:
self.tablets = tablets


class Tablet(db.Model, GlyphMixin):
# PK column and tablename etc. come from the mixin
area = db.Column(db.String(100), nullable=False, unique=True)
# association proxy
correspondents = association_proxy('tablet_correspondents', 'correspondent')

def __init__(self, area, correspondents=None):
self.area = area
if correspondents:
self.correspondents = correspondents


class Tablet_Correspondent(db.Model):

__tablename__ = "tablet_correspondent"
tablet_id = db.Column("tablet_id",
db.Integer(), db.ForeignKey("tablet.id"), primary_key=True)
correspondent_id = db.Column("correspondent_id",
db.Integer(), db.ForeignKey("correspondent.id"), primary_key=True)
# relations
tablet = db.relationship(
"Tablet",
backref="tablet_correspondents",
cascade="all, delete-orphan",
single_parent=True)
correspondent = db.relationship(
"Correspondent",
backref="correspondent_tablets",
cascade="all, delete-orphan",
single_parent=True)

def __init__(self, tablet=None, correspondent=None):
self.tablet = tablet
self.correspondent = correspondent

我可以将记录添加到 tabletcorrespondent 中,例如Tablet.query.first().correspondents 只是返回一个空列表,正如您所期望的那样。如果我使用现有的平板电脑和通讯员 ID 手动将一行插入到我的 tablet_correspondent 表中,该列表将再次如您所料地填充。

但是,如果我尝试这样做

cor = Correspondent.query.first()
tab = Tablet.query.first()
tab.correspondents.append(cor)

我得到:

KeyError: 'tablet_correspondents'

我很确定我在这里遗漏了一些相当基本的东西。

最佳答案

您的代码的问题.__init__ 方法中。如果你要 debug-watch/print() 参数,你会注意到参数 tablet 实际上是 Correspondent 的一个实例:

class Tablet_Correspondent(db.Model):
def __init__(self, tablet=None, correspondent=None):
print "in __init__: ", tablet, correspondent
self.tablet = tablet
self.correspondent = correspondent

原因在于 SA 创造新值(value)的方式。来自文档 Creation of New Values :

When a list append() event (or set add(), dictionary __setitem__(), or scalar assignment event) is intercepted by the association proxy, it instantiates a new instance of the “intermediary” object using its constructor, passing as a single argument the given value.

在您调用 tab.correspondents.append(cor) 的情况下,将使用单个参数 cor 调用 Tablet_Correspondent.__init__

解决方案?如果您只想将通讯员添加到Tablet,那么只需切换__init__ 。其实完全去掉第二个参数即可。
但是,如果您还将使用 cor.tablets.append(tab),那么您需要明确地使用 association_proxycreator 参数> 如上面链接的文档中所述:

class Tablet(db.Model, GlyphMixin):
# ...
correspondents = association_proxy('tablet_correspondents', 'correspondent', creator=lambda cor: Tablet_Correspondent(correspondent=cor))

class Correspondent(db.Model, GlyphMixin):
# ...
tablets = association_proxy('correspondent_tablets', 'tablet', creator=lambda tab: Tablet_Correspondent(tablet=tab))

关于python - 将对象添加到 SQLAlchemy 关联对象时出现 KeyError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11091491/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com