SQLAlchemy AttributeError: 'Query' object has no attribute '_sa_instance_state' when retrieving from database
SQLAlchemy AttributeError: 'Query' object has no attribute '_sa_instance_state' when retrieving from database
问题是尝试使用 Pyramid 上的 SQLAlchemy 从数据库中检索具有关系的对象。我想要的基本上是创建我需要从数据库中检索的对象,以完成网页所需的数据。
当我尝试访问 url /poll/{id}(使用有效的轮询 ID,例如:/poll/1)以获取页面时,我收到此错误:AttributeError: 'Query' 对象没有属性“_sa_instance_state”。怎么了?
这是模型的相关部分:
class Question(Base):
__tablename__ = 'question'
id = Column(Integer, primary_key=True)
text = Column(String(250))
type_id = Column(Integer, ForeignKey('type.id'))
type = relationship(Type)
poll_id = Column(Integer, ForeignKey('poll.id'))
poll = relationship(Poll)
def __init__(self, text, type, poll):
self.text = text
self.type = type
self.poll = poll
class Option(Base):
__tablename__ = 'option'
id = Column(Integer, primary_key=True)
text = Column(String(250))
question_id = Column(Integer, ForeignKey('question.id'))
question = relationship(Question)
def __init__(self, text, question):
self.text = text
self.question = question
这是给我带来麻烦的代码部分。调试器指向倒数第二行(选项对象)。
if request.matchdict['id'] != None:
pinst = session.query(Poll).get(request.matchdict['id'])
typeq = session.query(Type).first()
qinst = session.query(Question).filter_by(poll=pinst)
lopt = session.query(Option).filter_by(question=qinst)
return {'question':qinst, 'arroptions':lopt, 'type':typeq}
提前致谢!
qinst
是 Query
,而不是 Question
。你可能想要:
qinst = session.query(Question).filter_by(poll=pinst).one()
或
qinst = session.query(Question).filter_by(poll=pinst).first()
您还可以在 Question
上添加反向引用,这样您就可以从 Poll
转到 Question
:
class Question(Base):
...
poll = relationship(Poll, backref="question")
qinst = pinst.question
问题是尝试使用 Pyramid 上的 SQLAlchemy 从数据库中检索具有关系的对象。我想要的基本上是创建我需要从数据库中检索的对象,以完成网页所需的数据。
当我尝试访问 url /poll/{id}(使用有效的轮询 ID,例如:/poll/1)以获取页面时,我收到此错误:AttributeError: 'Query' 对象没有属性“_sa_instance_state”。怎么了?
这是模型的相关部分:
class Question(Base):
__tablename__ = 'question'
id = Column(Integer, primary_key=True)
text = Column(String(250))
type_id = Column(Integer, ForeignKey('type.id'))
type = relationship(Type)
poll_id = Column(Integer, ForeignKey('poll.id'))
poll = relationship(Poll)
def __init__(self, text, type, poll):
self.text = text
self.type = type
self.poll = poll
class Option(Base):
__tablename__ = 'option'
id = Column(Integer, primary_key=True)
text = Column(String(250))
question_id = Column(Integer, ForeignKey('question.id'))
question = relationship(Question)
def __init__(self, text, question):
self.text = text
self.question = question
这是给我带来麻烦的代码部分。调试器指向倒数第二行(选项对象)。
if request.matchdict['id'] != None:
pinst = session.query(Poll).get(request.matchdict['id'])
typeq = session.query(Type).first()
qinst = session.query(Question).filter_by(poll=pinst)
lopt = session.query(Option).filter_by(question=qinst)
return {'question':qinst, 'arroptions':lopt, 'type':typeq}
提前致谢!
qinst
是 Query
,而不是 Question
。你可能想要:
qinst = session.query(Question).filter_by(poll=pinst).one()
或
qinst = session.query(Question).filter_by(poll=pinst).first()
您还可以在 Question
上添加反向引用,这样您就可以从 Poll
转到 Question
:
class Question(Base):
...
poll = relationship(Poll, backref="question")
qinst = pinst.question