1

I have a Flask application which uses Flask-SQLAlchemy to connect to a database.

I need to check if row with name='reza' exists...

I think I can use Any(), Exists().

0

1 Answer 1

9
user = User.query.filter_by(name='reza').count()
if count > 0:
    print 'user exists'
Sign up to request clarification or add additional context in comments.

2 Comments

count() is a super-inefficient way of checking the existence of an object. There is an exist() function in SQLAlchemy with this purpose. Check this answer: stackoverflow.com/a/16003360/1674908
@Hamlett You are correct, this is the best way to do it - I wasn't aware of this function at the time.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.