在Pyramid,SQLAlchemy中将对象添加到一对多关系

时间:2014-01-13 15:15:40

标签: python sqlalchemy one-to-many pyramid

我已经建立了一个简单的系统,用于使用一对多关系(一个项目可以有很多注释)向我的数据库中的项目添加注释,但我似乎无法让它工作。我知道,它应该很简单。这可能是我忽略的简单事情。我非常赞赏第二眼。有人可以帮忙吗?

我有一个用SQLAlchemy定义的模型,如下所示:

class Item(Base):
    __tablename__ = 'items'  # Parent
    id = Column(Integer, primary_key=True)
    comments = relationship("Comment", backref='items')

class Comment(Base):
    __tablename__ = 'comments'  # Child
    id = Column(Integer, primary_key=True)
    comment_text = Column(Text, nullable=False)
    item_id = Column(Integer, ForeignKey('items.id'), nullable=False)

    def __init__(self, comment_text, item_id):
        self.comment_text = comment_text
        self.item_id = item_id

在我看来,我做了一些工作,然后尝试添加评论对象:

item = DBSession.query(Item).filter(Item.id == item_id).first()

try:
    print('Item id:', item.id, 'Comment text:', comment)
    print('Item Comments:', item.comments)
    cm = Comment(comment_text=comment,
                 item_id=item.id)
    print('a')
    item.comments.append(cm)
    #DBSession.add(cm)
    print('b')
    DBSession.commit()
except:
    DBSession.rollback()
    print('c')

请注意,我尝试了相同结果的item.comments.append(cm)和DBSession.add(cm)。这就是为什么在上面的代码块中注释掉了其中一个的原因。我也尝试了item.comments.append(评论(...)),结果相同。

现在,当我尝试添加评论时,我得到一个堆栈跟踪,最终得到:

sqlalchemy.exc.ResourceClosedError: This transaction is closed

整个跟踪,包括调试打印,如下所示:

Item id: 1 Comment text: test
Item Comments: []
a
c
Traceback (most recent call last):
  File "C:\Python33\lib\wsgiref\handlers.py", line 137, in run
    self.result = application(self.environ, self.start_response)
  File "C:\Users\[user]\PYTHON~1.5\lib\site-packages\pyramid\router.py", line 251, in __call__
    response = self.invoke_subrequest(request, use_tweens=True)
  File "C:\Users\[user]\PYTHON~1.5\lib\site-packages\pyramid\router.py", line 227, in invoke_subrequest
    response = handle_request(request)
  File "C:\Users\[user]\PYTHON~1.5\lib\site-packages\pyramid\tweens.py", line 21, in excview_tween
    response = handler(request)
  File "C:\Users\[user]\PYTHON~1.5\lib\site-packages\pyramid_tm\__init__.py", line 82, in tm_tween
    reraise(*exc_info)
  File "C:\Users\[user]\PYTHON~1.5\lib\site-packages\pyramid_tm\compat.py", line 13, in reraise
    raise value
  File "C:\Users\[user]\PYTHON~1.5\lib\site-packages\pyramid_tm\__init__.py", line 70, in tm_tween
    manager.commit()
  File "C:\Users\[user]\PYTHON~1.5\lib\site-packages\transaction\_manager.py", line 111, in commit
    return self.get().commit()
  File "C:\Users\[user]\PYTHON~1.5\lib\site-packages\transaction\_transaction.py", line 280, in commit
    reraise(t, v, tb)
  File "C:\Users\[user]\PYTHON~1.5\lib\site-packages\transaction\_compat.py", line 55, in reraise
    raise value
  File "C:\Users\[user]\PYTHON~1.5\lib\site-packages\transaction\_transaction.py", line 271, in commit
    self._commitResources()
  File "C:\Users\[user]\PYTHON~1.5\lib\site-packages\transaction\_transaction.py", line 417, in _commitResources
    reraise(t, v, tb)
  File "C:\Users\[user]\PYTHON~1.5\lib\site-packages\transaction\_compat.py", line 55, in reraise
    raise value
  File "C:\Users\[user]\PYTHON~1.5\lib\site-packages\transaction\_transaction.py", line 394, in _commitResources
    rm.tpc_vote(self)
  File "C:\Users\[user]\PYTHON~1.5\lib\site-packages\zope\sqlalchemy\datamanager.py", line 100, in tpc_vote
    self.tx.commit()
  File "C:\Users\[user]\PYTHON~1.5\lib\site-packages\sqlalchemy\orm\session.py", line 352, in commit
    self._assert_active(prepared_ok=True)
  File "C:\Users\[user]\PYTHON~1.5\lib\site-packages\sqlalchemy\orm\session.py", line 203, in _assert_active
    raise sa_exc.ResourceClosedError(closed_msg)
sqlalchemy.exc.ResourceClosedError: This transaction is closed

1 个答案:

答案 0 :(得分:1)

好吧,事实证明问题是一些损坏的文件,而不是编程错误。叹。好吧,问题解决了。 :)