I have built a CRUD API
for working with a database
. The Retrieve
(or Read) function
for the table class
, assessment_results
is currently giving me issues. Background regarding the design: the API
also has a convenience file that inherits from the main file (object.py) for purposes of accepting string
values.
What it needs to do: check for an ID
and/or check for two arguments (should the ID not be in use). If one or the other(s) if appropriate, retrieve it along with its particulars.
Following the docs on Python, I introduced the argument *
to allow optional position arguments. I also had helped from this great tutorial. (I am a newbie at designing and building dbs and using python.)
I believe the retrieve method is working, but there must be a flaw in the design or the method for create_category_rating(...)
as the Traceback
is returning now a ValueError
. I read here on Stacks (a similar issue) that I might need to assign a value to args, but it seems like I am through the API.
Code as Follows:
object.py
(retrieve method and create_category_rating method):
def retrieve_assessment_results(self, something_unique, *args):
if isinstance(something_unique, int):
return self.session.query(Assessment_Results).\
filter(Assessment_Results.id == something_unique).one()
elif isinstance(something_unique, basestring) and isinstance(args, basestring):
return self.session.query(Assessment_Results).\
filter(Assessment_Results.owner == something_unique).\
filter(Assessment_Results.assessment == args).one()
elif isinstance(something_unique, Assessment_Results) and isinstance(args, Assessment_Results):
return something_unique, args
else:
raise ValueError('Value being passed is an object')
def create_category_rating(self, category_rating_int, category, assessment_results):
new_catrating = Category_Rating(category_rating_int, category, assessment_results)
self.session.add(new_catrating)
print(new_catrating)
self.session.commit()
return(new_catrating)
convenience.py (inherits from object.py)
def create_category_rating(self, category_rating_int, category_name, username, name):
category = self.retrieve_category(category_name)
owner = self.retrieve_user(username)
assessment = self.retrieve_assessment(name)
assessment_results = self.retrieve_assessment_results(owner, assessment)
return super(ConvenienceAPI, self).create_category_rating(category_rating_int, category, assessment_results)
test.py
api = ConvenienceAPI()
api.create_category_rating(2, 'Decision-Making', 'baseball', 'Becoming a Leader')
Current Issue: StatementError
As per suggested, changed conditionals (needed to unpack and assign the tuple given by *args).
New API:
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 assessment is not None:
return self.session.query(Assessment_Results).\
filter(Assessment_Results.assessment == assessment).one()
elif owner is not None:
return self.session.query(Assessment_Results).\
filter(Assessment_Results.owner == owner).one()
TRACEBACK:
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 323, in retrieve_assessment_results
filter(Assessment_Results.assessment == assessment).one()
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2472, in one
ret = list(self)
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2515, in __iter__
return self._execute_and_instances(context)
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2530, in _execute_and_instances
result = conn.execute(querycontext.statement, self._params)
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 914, in execute
return meth(self, multiparams, params)
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/sql/elements.py", line 323, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1010, in _execute_clauseelement
compiled_sql, distilled_params
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1078, in _execute_context
None, None)
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1339, in _handle_dbapi_exception
exc_info
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/util/compat.py", line 199, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb)
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1073, in _execute_context
context = constructor(dialect, self, conn, *args)
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 544, in _init_compiled
grp, m in enumerate(parameters)]
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py", line 498, in construct_params
pd[self.bind_names[bindparam]] = bindparam.effective_value
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/sql/elements.py", line 1162, in effective_value
return self.callable()
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py", line 1386, in _go
value = fn(*arg, **kw)
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py", line 2422, in _get_state_attr_by_column
return state.manager[prop.key].impl.get(state, dict_, passive=passive)
StatementError: (exceptions.KeyError) 'assessment_id' [SQL: u'SELECT assessment_results.id AS assessment_results_id, assessment_results.created_on AS assessment_results_created_on, assessment_results.owner_id AS assessment_results_owner_id, assessment_results.assessment_id AS assessment_results_assessment_id \nFROM assessment_results \nWHERE ? = assessment_results.assessment_id'] [parameters: [immutabledict({})]]