Peewee is a simple yet powerful Object Relational Mapping (ORM) library for Python. It provides a convenient way to interact with databases by abstracting the underlying database-specific details.
When working with databases, it is common to have default values for columns or enforce constraints on the data being inserted. In this blog post, we will explore how to set default values and constraints in Peewee.
Setting Default Values
Default values are used to provide a fallback value for a column when no value is explicitly specified during an insert operation. Peewee allows you to set default values for columns using the default
parameter in the field definition.
Let’s say we have a User
model with a status
column that defaults to 'active'
:
from peewee import Model, CharField, TextField
class User(Model):
username = CharField()
status = CharField(default='active')
bio = TextField()
class Meta:
database = db
In the above example, if we insert a new user without specifying the status
column, it will automatically be set to 'active'
. However, if we explicitly set a value for the status
column during the insert, the specified value will be used instead.
Constraints
Constraints are rules applied to the database to ensure the integrity of the data being inserted or updated. Peewee supports various constraints such as NOT NULL
, UNIQUE
, PRIMARY KEY
, CHECK
, and FOREIGN KEY
.
NOT NULL Constraint
The NOT NULL
constraint ensures that a column cannot have a NULL
value. In Peewee, you can specify the null
parameter in the field definition to enforce the NOT NULL
constraint:
from peewee import Model, CharField
class User(Model):
username = CharField(unique=True)
password = CharField(null=False)
class Meta:
database = db
In the above example, the username
column is unique and the password
column must not be NULL
during insert or update operations.
UNIQUE Constraint
The UNIQUE
constraint ensures that the values in a column are unique. Peewee provides the unique
parameter to set the UNIQUE
constraint on a column:
from peewee import Model, CharField
class User(Model):
email = CharField(unique=True)
username = CharField(unique=True)
class Meta:
database = db
In the above example, both the email
and username
columns have the UNIQUE
constraint.
CHECK Constraint
The CHECK
constraint allows you to specify a condition that must be true for the data being inserted or updated. Peewee supports the constraints
parameter to add a CHECK
constraint:
from peewee import Model, CharField
class User(Model):
username = CharField()
age = IntegerField(constraints=[Check('age > 0')])
class Meta:
database = db
In the above example, the age
column has a CHECK
constraint that ensures the age is greater than 0
.
Conclusion
In this blog post, we explored how to set default values and constraints in Peewee. Setting default values and constraints are essential for maintaining data integrity and enforcing business rules. With Peewee, you can easily define default values and constraints for your database columns, making it easier to work with data in your applications.