我最近遇到了这个问题,我已经坚持了几天了。基本上,这里感兴趣的两个表是“Activity”和“Goal”,其中 Activity 是 Goal 的单亲;一个目标只有一个父级,一个活动有一个或多个目标子级:
class Activity(Base, Table):
__tablename__ = 'activity'
pk_id = Column(Integer, primary_key=True)
name = Column(String)
wave = Column(Integer)
enabled = Column(Boolean)
goals = relationship("Goal", backref='activity', single_parent=True, cascade="all,delete-orphan")
activity_tags = relationship("ActivityTags", backref='tag_activity', cascade="all,delete-orphan")
和
class Goal(Base, Table):
__tablename__ = 'goal'
pk_id = Column(Integer, primary_key=True)
activity_id = Column(Integer, ForeignKey('activity.pk_id'))
category_id = Column(Integer, ForeignKey('category.pk_id'))
name = Column(String)
minutes_expected = Column(Integer)
date_last_invoked = Column(Date)
date_last_invalidated = Column(Date)
enabled = Column(Boolean)
milestones = relationship("Milestone", backref='goal', single_parent=True, cascade="all,delete-orphan")
现在,当我创建一个 Activity 对象(进而创建 1 个 Goal 子对象)时,我遇到了这个错误:
Traceback (most recent call last):
File "unit.py", line 49, in wrapper
r = func(*args, **kwargs)
File "unit.py", line 158, in testActivityWaveOverdue
a = Activity(c, 'Pull-Ups', wave)
File "<string>", line 4, in __init__
File "/home/me/dev/coach/.venv/lib/python2.7/site-packages/sqlalchemy/orm/state.py", line 98, in initialize_instance
return manager.original_init(*mixed[1:], **kwargs)
File "/home/me/dev/coach/coach/activity.py", line 64, in __init__
self.add_goal(category, 'maintenance')
File "/home/me/dev/coach/coach/activity.py", line 190, in add_goal
return Goal(self, category, name, minutes_expected)
File "<string>", line 4, in __init__
File "/home/me/dev/coach/.venv/lib/python2.7/site-packages/sqlalchemy/orm/state.py", line 98, in initialize_instance
return manager.original_init(*mixed[1:], **kwargs)
File "/home/me/dev/coach/coach/goal.py", line 76, in __init__
db.session.commit()
File "/home/me/dev/coach/.venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 656, in commit
self.transaction.commit()
File "/home/me/dev/coach/.venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 314, in commit
self._prepare_impl()
File "/home/me/dev/coach/.venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 298, in _prepare_impl
self.session.flush()
File "/home/me/dev/coach/.venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 1583, in flush
self._flush(objects)
File "/home/me/dev/coach/.venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 1636, in _flush
is_orphan = _state_mapper(state)._is_orphan(state) and state.has_identity
File "/home/me/dev/coach/.venv/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py", line 1237, in _is_orphan
state, key, optimistic=bool(state.key)):
File "/home/me/dev/coach/.venv/lib/python2.7/site-packages/sqlalchemy/orm/instrumentation.py", line 331, in has_parent
return self.get_impl(key).hasparent(state, optimistic=optimistic)
File "/home/me/dev/coach/.venv/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py", line 349, in hasparent
assert self.trackparent, "This AttributeImpl is not configured to track parents."
AssertionError: This AttributeImpl is not configured to track parents.
这是活动初始化代码:
def __init__(self, category, name, wave):
self.name = name
self.wave = wave
self.enabled = True
db.session.add(self)
db.session.commit()
self.add_goal(category, 'maintenance')
def add_goal(self, category, name, minutes_expected=30):
return Goal(self, category, name, minutes_expected)
还有目标:
def __init__(self, activity, category, name, minutes_expected):
self.activity_id = activity.pk_id
self.category_id = category.pk_id
self.name = name
self.minutes_expected = minutes_expected
self.date_last_invoked = START_OF_TIME
self.date_last_invalidated = START_OF_TIME
self.enabled = True
db.session.add(self)
db.session.commit()
如果您希望我在上面包含其他任何内容,请告诉我 - 我不想让帖子变得比需要的大,所以只包含我认为与问题相关的内容。
最后,Activity 和 Goal 继承自的“Table”超类只是提供了辅助/辅助方法。