How to use SQLAlchemy ORM to perform union with three queries In Python
SQLAlchemy ORM uses an object-centric view that encapsulates the schema with business objects. It is a more pythonic implementation as we can see the tables represented in the class format. We have used this class object to query the payment table using the SQLAlchemy syntax mentioned above. We created individual queries to extract record(s) from the table having payment_id as 1, 2, or 3. Then the first query which is of `sqlalchemy.orm.Query` type is chained using the union() method. The other two queries are passed as parameters in the union() method. One or more `sqlalachemy.orm.Query` type objects can be passed as parameters to this method.
Syntax: sqlalchemy.orm.Query.union(*q)¶
Produce a UNION of this Query against one or more queries.
Python3
from sqlalchemy.orm import sessionmaker import sqlalchemy as db from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() # DEFINE THE ENGINE (CONNECTIO OBJECT) engine = db.create_engine( "mysql+pymysql://root:password@localhost/sakila" ) # CREATE THE TABLE MODEL TO USE IT FOR QUERYING class Payment(Base): __table__ = db.Table( "payment" , Base.metadata, autoload_with = engine) # CREATE A SESSION OBJECT TO INITIATE QUERY IN DATABASE Session = sessionmaker(bind = engine) session = Session() # PREPARING QUERY USING SQLALCHEMY query_1 = session.query(Payment). filter (Payment.payment_id = = 1 ) query_2 = session.query(Payment). filter (Payment.payment_id = = 2 ) query_3 = session.query(Payment). filter (Payment.payment_id = = 3 ) # EXTRACT ALL THE RECORDS BY PERFORMING UNION OF THREE QUERIES result = query_1.union(query_2, query_3). all () # PRINT THE RESULTANT RECORDS for r in result: print (r.payment_id, "|" , r.customer_id, "|" , r.rental_id, "|" , r.amount) |
Output:
Python SQLAlchemy – Performing union with three queries
In this article, we will see how to perform a union of three queries using SQLAlchemy in Python.
Since we are going to use MySQL in this post, we will also install a SQL connector for MySQL in Python. However, none of the code implementations changes with change in the database except for the SQL connectors.
pip install pymysql
We will use the sample sakila database from MySQL. In this article, we will cover 2 examples, one each for SQLAchemy Core and ORM layers. In both examples, we will extract the records from the payment table within the sakila database which have a `payment_id` of 1, 2, or 3 (as individual queries and then take the union of them). If you do not have the sakila database and want to follow along with this article without installing it then use the SQL script present in the link mentioned below to create the required schema and payment table along with the records.
Sakila Payment Table Script
For reference, the first ten records in the payment table look like this –
The SQL query which we are looking at in the below two examples is –
SELECT * FROM payment WHERE payment_id = 1 UNION SELECT * FROM payment WHERE payment_id = 2 UNION SELECT * FROM payment WHERE payment_id = 3;