I`m using flask-sqlalchemy and my problem is where in clause like:
select * from table_1 where id in (1,2,3,5)
OR
select * from table_1 where field_1_id in (select id from table_2 where .... )
and get_or_create like peewee orm
Object.get_or_create(.......)
how can i generate this sentences with flask-sqlalchemy ?
All SELECT statements generated by SQLAlchemy ORM are constructed by Query object. It provides a generative interface, hence successive calls return a new Query object, a copy of the former with additional criteria and options associated with it.
method sqlalchemy.orm.Query. all() Return the results represented by this Query as a list. This results in an execution of the underlying SQL statement.
The select() method of table object enables us to construct SELECT expression. The resultant variable is an equivalent of cursor in DBAPI. We can now fetch records using fetchone() method. Here, we have to note that select object can also be obtained by select() function in sqlalchemy.
SQLAlchemy Core The already created students table is referred which contains 4 columns, namely, first_name, last_name, course, score. But we will be only selecting a specific column. In the example, we have referred to the first_name and last_name columns. Other columns can also be provided in the entities list.
Try .in_ clause in sqlalchemy
result = db_session.query(table_1).filter(table_1.id.in_((1,2,3,5))).all()
Note: here am assuming table_1 is your model
Alternate you can also use the following syntax:
result = TableName.query.filter(TableName.id.in_([1,2,3]))
Note: I am using flask-sqlalchemy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With