我一直抱怨Traceback
抱怨系统找不到结果。这不应该是对象实际通过的情况(我知道这是因为我有一个打印命令来确认查询的内容)。我相信问题可以在create_category_rating(..)
的方法中找到。
当我使用.one()
时,它会抛出我在下面的投诉。当我使用.all
时,它会返回AttributeError: 'list' object has no attribute '_sa_instance_state'
(其他人有issue)。 SQLAlchemy的docs在定义这些方法方面做得很好,但由于某些原因我的代码崩溃了。
问题:如果对象正在通过retrieve方法,导致程序中断的原因是什么?某处有一个错误,我一直在努力压扁它。
我正在使用SQLAlchemy
和python2.7
:
追溯:
ERROR: notssdb.test.test.test1
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
self.test(*self.arg)
File "/Users/ack/code/venv/NotssDB/notssdb/test/test.py", line 111, in test1
api.create_category_rating(2, 'Decision-Making', 'baseball', 'Becoming a Leader')
File "/Users/ack/code/venv/NotssDB/notssdb/api/convenience.py", line 41, in create_category_rating
assessment_results = self.retrieve_assessment_results(owner, assessment)
File "/Users/ack/code/venv/NotssDB/notssdb/api/object.py", line 324, in retrieve_assessment_results
filter(Assessment_Results.owner == owner).one()
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2478, in one
raise orm_exc.NoResultFound("No row was found for one()")
NoResultFound: No row was found for one()
# output from Traceback
Decision-Making
baseball
Becoming a Leader
Becoming a Leader
test1 <Assessment(name='Becoming a Leader', text='better decisions')>
convenience.py (创建方法):
def create_category_rating(self, category_rating_int, category_name, username, name):
category = self.retrieve_category(category_name)
owner = self.retrieve_user(username) # added
assessment = self.retrieve_assessment(name) #added
assessment_results = self.retrieve_assessment_results(owner, assessment)
return super(ConvenienceAPI, self).create_category_rating(category_rating_int, category, assessment_results)
object.py (检索方法):
def retrieve_assessment_results(self, *args):
id, assessment, owner = None, None, None
if len(args) == 1:
id, = args[0]
elif len(args) == 2:
assessment, owner = args
else:
raise ValueError('Value being passed is an object')
if id is not None:
return self.session.query(Assessment_Results).\
filter(Assessment_Results.id == id).one()
elif owner is not None:
print 'test1', owner
return self.session.query(Assessment_Results).\
filter(Assessment_Results.owner == owner).one()
elif assessment is not None:
print 'test2', assessment
return self.session.query(Assessment_Results).\
filter(Assessment_Results.assessment == assessment).one()
答案 0 :(得分:3)
one()
将抛出异常。使用first()
,如果没有结果,您将获得第一个结果或Python None
。
同样关于all()
,因为该问题不包含相关的追溯和代码。
答案 1 :(得分:3)
您有以下内容:
self.retrieve_assessment_results(owner, assessment)
...但是“retrieve_assessment_results”的参数如下(添加了我的评论)......
def retrieve_assessment_results(self, *args):
id, assessment, owner = None, None, None
if len(args) == 1:
id, = args[0]
elif len(args) == 2:
assessment, owner = args # Note this line!
else:
raise ValueError('Value being passed is an object')
你通过所有者,然后评估。但如果您检测到两个对象,您将阅读评估,然后是所有者。换句话说,您使用一个对象顺序调用方法,并假设反向顺序检索。您甚至可以在调试行中看到这种情况:
print 'test1', owner
您希望打印出像test1 <User(...)>
这样的内容,而不是打印出来......
test1 <Assessment(name='Becoming a Leader', text='better decisions')>
另外,请考虑使用Python keyword arguments代替:
def retrieve_assessment_results(self, id=None, assessment=None, owner=None):
...
然后你的电话变得不那么模糊:
self.retrieve_assessment_results(owner=owner, assessment=assessment)