0

I'm trying to do the following in Flask-SQLAlchemy:

SELECT recipe_id
FROM UserRecipe
WHERE
  user_id == 3
 AND
  status != 'donotshow'

This is the best I've come up with so far:

user = User.query.filter_by(url=url).first_or_404()
userrecipes = UserRecipe.query.filter(user_id==user.id, status!='donotshow').with_entities(UserRecipe.recipe_id)

I get an error:

NameError: name 'user_id' is not defined

Would really appreciate some help :)

Muriel
  • 449
  • 4
  • 20
  • Does this answer your question? [Why do I get NameError for the column that is defined in SQLAlchemy model](https://stackoverflow.com/questions/50223179/why-do-i-get-nameerror-for-the-column-that-is-defined-in-sqlalchemy-model) – Christopher Peisert Oct 24 '20 at 14:15

1 Answers1

1

With filter(*criterion) you need to qualify the column attributes with the class name:

userrecipes = UserRecipe.query.filter(UserRecipe.user_id==user.id, 
    UserRecipe.status!='donotshow').with_entities(UserRecipe.recipe_id)

By contrast, filter_by(**kwargs) accepts unqualified column attributes as keyword arguments:

userrecipes = UserRecipe.query.filter_by(user_id==user.id, 
    status!='donotshow').with_entities(UserRecipe.recipe_id)
Christopher Peisert
  • 21,862
  • 3
  • 86
  • 117