Peewee is a lightweight Object Relational Mapping (ORM) library for Python. It is widely used for interacting with databases in a Python application. Peewee supports multiple databases, allowing you to work with different database systems simultaneously. This capability is particularly useful in scenarios where you need to connect to and work with data from multiple databases in your application.
In this blog post, we will explore how to use Peewee to connect to and interact with multiple databases simultaneously in Python.
Connecting to Multiple Databases
To connect to multiple databases using Peewee, you need to define multiple database objects. Each database object represents a separate database connection. You can create these objects using the SqliteDatabase
class provided by Peewee. Let’s see an example below:
from peewee import SqliteDatabase, Model
db1 = SqliteDatabase('database1.db')
db2 = SqliteDatabase('database2.db')
class BaseModel(Model):
class Meta:
database = db1 # Set the default database
class Model1(BaseModel):
# Define the fields for Model1
class Meta:
database = db1 # Set the database for this model
class Model2(BaseModel):
# Define the fields for Model2
class Meta:
database = db2 # Set the database for this model
In the above example, we create two SqliteDatabase
objects, db1
and db2
, representing two separate databases. Then, we define our models Model1
and Model2
inheriting from a BaseModel
class that specifies the default database as db1
. However, we explicitly set the database for Model2
as db2
using the Meta
class.
Performing Operations on Multiple Databases
Once you have set up connections to multiple databases, you can perform various operations on each database individually. For example, you can create tables, query data, and perform CRUD (Create, Read, Update, Delete) operations. Let’s see some examples below:
Creating Tables
To create tables in each database, you can use the create_table
method provided by each model. Here’s an example:
# Create tables in db1
with db1:
db1.create_tables([Model1])
# Create tables in db2
with db2:
db2.create_tables([Model2])
In the above example, we use the create_tables
method within a with
block to create tables in each database.
Querying Data
To query data from each database, you can use the select
method provided by each model. Here’s an example:
# Query data from db1
with db1:
query = Model1.select().where(Model1.field == 'value')
results = [item.field for item in query]
# Query data from db2
with db2:
query = Model2.select().where(Model2.field == 'value')
results = [item.field for item in query]
In the above example, we use the select
method within a with
block to query data from each database.
CRUD Operations
You can perform CRUD operations on each database by creating, updating, and deleting records. Here’s an example:
# Creating a record in db1
with db1:
new_record = Model1.create(field='value')
new_record.save()
# Updating a record in db2
with db2:
record_to_update = Model2.get(Model2.field == 'old_value')
record_to_update.field = 'new_value'
record_to_update.save()
# Deleting a record in db1
with db1:
record_to_delete = Model1.get(Model1.id == 1)
record_to_delete.delete_instance()
In the above example, we perform different CRUD operations on each database within separate with
blocks.
Conclusion
Peewee provides a convenient way to connect to and work with multiple databases simultaneously in Python. By defining separate database objects within your application and specifying different databases for your models, you can perform operations on each database independently. This capability enables effective management of disparate databases within a single Python application.
I hope this guide helps you understand how to use Peewee to work with multiple databases simultaneously. Happy coding!