python sqlalchemy AttributeError: 'BaseQuery' object has no attribute 'id'
broccoli beans
I am trying to set the foreign_key column of a table by using data from another table "Nutritional Values"
class NutritionalValues(db.Model):
__tablename__ = 'nutritionalvalues'
id = db.Column(db.Integer, primary_key=True)
item = db.Column(db.String(200), nullable=False)
calories = db.Column(db.Float, nullable=False)
totalfat = db.Column(db.Float, nullable=False)
by using
consumed_nutrionalvalue_id = NutritionalValues.query.filter_by(item=consumed_item).id
where "consumed_item" is some string that exactly matches the string of the "item" value of a row of the NutritionalValues table
but i get the error
AttributeError: 'BaseQuery' object has no attribute 'id'
Traceback (most recent call last)
File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/flask/app.py", line 2463, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/flask/app.py", line 2449, in wsgi_app
response = self.handle_exception(e)
File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/flask/app.py", line 1866, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/benjamattesjaroen/helloPython/app.py", line 65, in index
consumed_nutrionalvalue_id = NutritionalValues.query.filter_by(item=consumed_item).id
AttributeError: 'BaseQuery' object has no attribute 'id'
But does the explicit "nutritive value" have a column called "id"? How can I access the integers stored in the lookup table?
Andrew Allen
Below is the query
NutritionalValues.query.filter_by(item=consumed_item)
To get the object (this o is in the orm) you can use .first()
or get many results.all()
NutritionalValues.query.filter_by(item=consumed_item).first()
Now you have a normal object on which you can perform pythonic methods such as member access.
This is interesting because you can build dynamic queries before execution to get the results.