我被困在这个问题上,不知道如何解决。这是我的 models.py
文件:
模型.py
class TripDetail(db.Model):
"""
Base class for every table that contains info about a trip.
"""
__abstract__ = True
__bind_key__ = 'instructions'
id = db.Column(db.Integer, primary_key=True)
# list of fields
class Overview(TripDetail):
"""
Class that contains general information about a trip.
"""
__tablename__ = 'overviews'
__table_args__ = (
db.ForeignKeyConstraint(['user_id', 'calendar_id'], ['calendars.user_id', 'calendars.id'], ondelete='CASCADE'),
) # constraints on other tables, omitted here
user_id = db.Column(db.Integer, primary_key=True)
calendar_id = db.Column(db.Integer, primary_key=True)
calendar = db.relationship('Calendar', backref=db.backref('overviews', cascade='delete'), passive_deletes=True)
# other fields
class Step(TripDetail):
__tablename__ = 'steps'
overview_id = db.Column(db.Integer, db.ForeignKey('overviews.id', ondelete='CASCADE'))
overview = db.relationship('Overview', backref=db.backref('steps', cascade='delete'), passive_deletes=True)
# also other fields
这就是我将项目添加到数据库的方式(响应参数包含一个与类匹配的字典,以这种方式可以直接解包):
def add_instruction(response):
"""
Adds a travel instruction to the database.
"""
steps = response.pop('steps')
overview = Overview(**response)
for step in steps:
Step(overview=overview, **step)
db.session.add(overview)
db.session.commit()
logger.info(f"Stored instruction with PK {(overview.id, overview.user_id, overview.calendar_id, overview.event_id)}")
现在,overviews
表已正确填写,但 steps
仍为空。检查日志,我收到此警告:
SAWarning: Object of type not in session, add operation along 'Overview.steps' will not proceed (orm_util.state_class_str(state), operation, prop))
我做错了什么?
最佳答案
通常,当 add()
将对象添加到 session 时,它们的相关对象将按照您的需要自动添加。该行为由关系的 cascade
控制。 .
在 Steps.overview
中将 cascade
设置为 'delete'
会删除默认的 'save-update'
,这就是打开自动添加的原因。您可以使用cascade='save-update, delete'
将其添加回去,但请查看possible traits并查看您可能还需要什么。一个常见的集合是'all, delete-orphan'
。
请记住这些是严格的 ORM 行为;在级联中设置 'delete'
不会设置列的 ON [event] CASCADE
。
关于python - SAWarning : Object of type <Child> not in session, 沿着 'Parent.children' 的添加操作将不会继续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47912520/