2014-01-13 53 views
0

我已經建立了一個簡單的系統,用於使用一對多關係(一個項目可以有很多註釋)向我的數據庫中的項目添加註釋,但我似乎無法讓它工作。我知道這應該很簡單。這可能很簡單,我忽略了。我會非常欣賞這第二套眼睛。誰能幫忙?在金字塔中添加一個對象到一對多關係中,SQLAlchemy

我有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(Comment(...)),結果相同。

現在,當我嘗試添加評論,我得到一個堆棧跟蹤,最終在:

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 
+0

嘗試 'DBSession.add(釐米) 打印( 'B') DBSession.flush()' –

+0

如何'Item'和'Log'車型有關係嗎? – van

+0

@van對不起,錯字。 – Etni3s

回答

1

好事實證明,問題是一些損壞的文件,而不是編程錯誤。嘆。那麼,問題解決了。 :)

+0

什麼文件被損壞?源代碼文件已被複制到您的服務器?數據庫本身?我有同樣的問題,沒有找到答案。 (環境:金字塔使用pyramid_tm,Zope)。其他數據庫事務從其他模型中正常工作。在同一個API調用中添加到幾個不同的表時會發生故障 – hamx0r

相關問題