[파이썬] Peewee UUIDField 활용

In this blog post, we will explore how to use the UUIDField in Peewee, a lightweight and expressive Python ORM (Object-Relational Mapping) library. The UUIDField is a field type that allows us to store and manipulate Universally Unique Identifiers (UUIDs) in our database models.

UUIDs are often used when we need a unique identifier that is guaranteed to be unique across different systems or instances. They are especially useful when working with distributed systems or when generating unique IDs for sensitive information.

Installing Peewee

Before we dive into using the UUIDField, we need to install the Peewee library. Open up your terminal and run the following command to install Peewee using pip:

pip install peewee

Creating a Model with a UUIDField

Let’s start by creating a simple model in Peewee that includes a UUIDField:

from peewee import *

database = SqliteDatabase('example.db')

class User(Model):
    id = UUIDField(primary_key=True)
    name = CharField()

    class Meta:
        database = database

database.connect()
database.create_tables([User])

In the code above, we import the peewee module and create a SqliteDatabase instance pointing to a file called example.db. Then, we define a User model with two fields: id and name. The id field is of type UUIDField and is set as the primary key.

Finally, we connect to the database and create the necessary table(s) by calling database.create_tables([User]).

Creating and Saving a User

To create and save a User instance with a UUID, we need to generate a UUID and assign it to the id field. We can use the uuid module from the Python standard library to generate a random UUID.

import uuid

# Generate a random UUID
user_id = uuid.uuid4()

# Create and save the User instance
user = User(id=user_id, name="John Doe")
user.save()

In the code above, we import the uuid module and use the uuid4() function to generate a random UUID. We then create a User instance by passing the generated UUID and the user’s name. Finally, we call the save() method to save the instance to the database.

Querying for Users

We can also query the database to retrieve User instances based on their UUID or other criteria. The following code demonstrates how to query for all users with a specific UUID:

users = User.select().where(User.id == user_id)

for user in users:
    print(user.name)

In the code above, we use the select() method to retrieve all the User instances from the database. We then filter the results using the where() method and specify the condition User.id == user_id. This will return all users with the specified UUID.

Conclusion

In this blog post, we explored how to use the UUIDField in Peewee to store and manipulate UUIDs in our database models. We learned how to create a model with a UUIDField, generate and save a User instance with a UUID, and query the database for users based on their UUID.

Using UUIDs can be helpful when we need unique identifiers that are universally unique and can be generated in a distributed environment. The UUIDField in Peewee makes it easy to work with UUIDs in our Python applications.