如何插入'答案'模型?

时间:2010-09-08 10:52:10

标签: sqlalchemy relationship

标题可能不完全,但我不知道如何表达。

我有3个课程:用户,问题,答案。简单的代码是:

Session = scoped_session(sessionmaker())
Base = declarative_base()

class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)

    questions = relationship('Question', backref="user")
    answers = relationship('Answer', backref="user")

class Question(Base):
     __tablename__ = 'questions'
     id = Column(Integer, primary_key=True)
     user_id = Column(Integer, ForeignKey('users.id'))

     answers = relationship('Answer', backref="user")

class Answer(Base):
     __tablename__ = 'answers'
     user_id = Column(Integer, ForeignKey('users.id'))
     question_id = Column(Integer, ForeignKey('questions.id'))

     id = Column(Integer, primary_key=True)

现在,用户提出问题,因此会创建一个答案:

user = get_user_from_session()
question = get_question(question_id)

# create answer
answer = Answer()
answer.user = user
answer.question = question

Session.add(answer)  # !!!
Session.commit()

我希望答案会被插入到数据库中,但不幸的是,报告了一个错误:

 AttributeError: 'module' object has no attribute '_sa_instance_state'

有什么我错过的吗?如何解决?


更新

感谢@dhaffey,我已经解决了错别字。我重新创建了一个测试文件来测试它,发现没有再次发生错误,但提交后answer.user_idanswer.question_id在数据库中为空。

这是我的代码,你可以直接运行它。

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import *

engine = create_engine('sqlite:///test.sqlite', echo=True)
Session = scoped_session(sessionmaker())
Base = declarative_base()

Base.metadata.bind=engine

class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    name = Column(String)
    questions = relationship('Question')
    answers = relationship('Answer')

class Question(Base):
    __tablename__ = 'questions'
    id = Column(Integer, primary_key=True)
    user_id = Column(Integer, ForeignKey('users.id'))
    title = Column(String)
    answers = relationship('Answer')

class Answer(Base):
    __tablename__ = 'answers'
    user_id = Column(Integer, ForeignKey('users.id'))
    question_id = Column(Integer, ForeignKey('questions.id'))

    id = Column(Integer, primary_key=True)

Base.metadata.create_all()

user = User()
user.name = 'aaa'
Session.add(user)
Session.flush()

question = Question()
question.title = 'ttt'
question.user = user
Session.add(question)
Session.flush()

answer = Answer()
answer.user = user
answer.question = question

Session.add(answer)
Session.commit()

print answer.id # not None

found = Session.query(Answer).filter_by(id=answer.id).one()

print found.user.name # not None
print found.question.title # not None

# !!! It seems all models are saved correctly,
# but please open the test.sqlite database, (not querying by sqlahchemy)
# the question.user_id and answer.user_id and answer.question_id are null

1 个答案:

答案 0 :(得分:2)

你的类声明不为我“编译”,所以我想知道你是否运行了这段代码,以及你正在使用哪个SQLAlchemy版本。这条线

user_id = Column(Integer, ForeignKey='users.id')

加注

sqlalchemy.exc.ArgumentError: Unknown arguments passed to Column: ['ForeignKey']

使用SQLAlchemy 0.6.4。您正在尝试使用关键字参数声明外键,但正确的用法是构造ForeignKey对象并按位置传递它,如下所示:

user_id = Column(Integer, ForeignKey('users.id'))

修复外键后,您的示例将按预期工作。

请注意,在适当声明相应的外键时,不需要在这些关系上显式提供primaryjoin参数 - SQLAlchemy推断出正确的连接。