3

I can get the SELECT id from parts where name='Part1' as an SQL statement but what is the way to do that with SQLAlchemy?

I've tried;

db.session.query(Parts.id).filter(name=form.name.data)

But this returns an Object not the id

2 Answers 2

6

Use the below code.

parts = db.session.query(Parts).filter(Parts.name==Part1).first()

Then if you want the only id you can just access

parts.id
0
1

Store that object in a varaible, and then use dot notation to pull out what you want. For example:

item = db.session.query(Parts.id).filter(name=form.name.data).one()
print item.id

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.