2

我正在使用以下模型设置设置一个 facebook 风格的消息传递系统:

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True)
    password = db.Column(db.String(12), unique=False)
    all_conversations = db.relationship('Conversation',
        primaryjoin='or_(User.id == Conversation.user1_id, User.id == ' \
        'Conversation.user2_id)', lazy='dynamic')
    # Replies where this is this user.
    replies = db.relationship("Reply", backref="user", lazy='dynamic')

class Conversation(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user1_id = db.Column(db.Integer, db.ForeignKey("user.id"))
    user2_id = db.Column(db.Integer, db.ForeignKey("user.id"))        
    user1 = db.relationship('User', foreign_keys=[user1_id], 
        backref='conversations_as_user1')       
    user2 = db.relationship('User', foreign_keys=[user2_id], 
        backref='conversations_as_user2')
    # Replies to this conversation.
    replies = db.relationship("Reply", backref="conversation", lazy='dynamic')


class Reply(db.Model):
    id = db.Column(db.Integer, primary_key = True)
    reply = db.Column(db.Text)
    user_id = db.Column(db.Integer, db.ForeignKey("user.id"))
    conversation_id = db.Column(db.Integer, db.ForeignKey("conversation.id"))

但是,每次我尝试将回复对象添加到数据库然后提交时,我都会收到以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "build/bdist.macosx-10.8-intel/egg/sqlalchemy/orm/scoping.py", line 149, in do
  File "build/bdist.macosx-10.8-intel/egg/sqlalchemy/orm/session.py", line 721, in commit
  File "build/bdist.macosx-10.8-intel/egg/sqlalchemy/orm/session.py", line 354, in commit
  File "build/bdist.macosx-10.8-intel/egg/sqlalchemy/orm/session.py", line 334, in _prepare_impl
  File "build/bdist.macosx-10.8-intel/egg/sqlalchemy/orm/session.py", line 1818, in flush
  File "build/bdist.macosx-10.8-intel/egg/sqlalchemy/orm/session.py", line 1936, in _flush
  File "build/bdist.macosx-10.8-intel/egg/sqlalchemy/util/langhelpers.py", line 58, in __exit__
  File "build/bdist.macosx-10.8-intel/egg/sqlalchemy/orm/session.py", line 1900, in _flush
  File "build/bdist.macosx-10.8-intel/egg/sqlalchemy/orm/unitofwork.py", line 372, in execute
  File "build/bdist.macosx-10.8-intel/egg/sqlalchemy/orm/unitofwork.py", line 525, in execute
  File "build/bdist.macosx-10.8-intel/egg/sqlalchemy/orm/persistence.py", line 64, in save_obj
  File "build/bdist.macosx-10.8-intel/egg/sqlalchemy/orm/persistence.py", line 569, in _emit_insert_statements
  File "build/bdist.macosx-10.8-intel/egg/sqlalchemy/engine/base.py", line 662, in execute
  File "build/bdist.macosx-10.8-intel/egg/sqlalchemy/engine/base.py", line 761, in _execute_clauseelement
  File "build/bdist.macosx-10.8-intel/egg/sqlalchemy/engine/base.py", line 874, in _execute_context
  File "build/bdist.macosx-10.8-intel/egg/sqlalchemy/engine/base.py", line 1024, in _handle_dbapi_exception
  File "build/bdist.macosx-10.8-intel/egg/sqlalchemy/util/compat.py", line 195, in raise_from_cause
  File "build/bdist.macosx-10.8-intel/egg/sqlalchemy/engine/base.py", line 867, in _execute_context
  File "build/bdist.macosx-10.8-intel/egg/sqlalchemy/engine/default.py", line 324, in do_execute
sqlalchemy.exc.InterfaceError 

通过进行一些测试,我发现,当我摆脱任何外键时,回复会发布到数据库中,但是其中一个和/或两个键都会导致它抛出这个错误。知道为什么会这样吗?

4

0 回答 0