我很困惑,我试图将子级添加到数据库中现有的父级对象中。当尝试这样做时,我得到一个错误。
我不确定如何确保保存新添加的子对象? 错误和我的尝试如下:
equityLinkedNote = db.session.query(EquityLinkedNote).filter(EquityLinkedNote.cmdb == cmdb_eln.cmdb)
underlier_list = underliers.to_dict(orient="records")
equityLinkedNote.Underlier =\
[UnderlierDetails(
Ticker=underlier["Ticker"] if "Ticker" in underlier else None,
Weight=underlier["Weight"] if "Weight" in underlier else None,
ReturnFloor=underlier["ReturnFloor"] if "ReturnFloor" in underlier else None,
InitialFixing=underlier["InitialFixing"] if "InitialFixing" in underlier else None,
ReturnCap=underlier["ReturnCap"] if "ReturnCap" in underlier else None,
EffectiveDate=underlier["EffectiveDate"] if "EffectiveDate" in underlier else cmdb_eln.trade_date
) for underlier in underlier_list]
db.session.flush()
db.session.commit()
错误
File "....\lib\site-packages\sqlalchemy\ext\declarative\base.py", line 747, in _declarative_constructor
setattr(self, k, kwargs[k])
File "...\site-packages\sqlalchemy\orm\attributes.py", line 229, in __set__
instance_dict(instance), value, None)
File "...\site-packages\sqlalchemy\orm\attributes.py", line 711, in set
state._modified_event(dict_, self, old)
AttributeError: 'NoneType' object has no attribute '_modified_event'
我不知道为什么会失败。任何帮助将不胜感激!
这是我的模特
class EquityLinkedNote(GenericSecMasterObject):
Underlier = db.relationship("UnderlierDetails", backref='note', cascade="save-update, merge, delete", lazy='dynamic',
collection_class=list)
下层
class UnderlierDetails(db.Model):
__tablename__ = "underlier_details"
id = db.Column(db.Integer, primary_key=True)
Ticker = db.Column(db.String(50), nullable=False)
InitialFixing = db.Column(db.Float, nullable=True)
ReturnCap = db.Column(db.Float, nullable=True)
ReturnFloor = db.Column(db.Float, nullable=True)
Weight = db.Column(db.Float, nullable=True)
EffectiveDate = db.Column(db.DateTime, nullable=False)
cmdb = db.Column(db.String(20), db.ForeignKey('sec_master_details.cmdb'), nullable=False)
答案 0 :(得分:0)
我意识到我的错误,对于任何有类似问题的人,请不要在db.Model中使用以下内容,将其删除并解决错误...
def __getattr__(self, item):
return None