Use Case Example of Sqlalchemy-Utils
Enumerate Priority Levels with SQLAlchemy-Utils
In below code, establishes an SQLite in-memory database and defines a Task model using SQLAlchemy-Utils’ ChoiceType to represent enumerated priority levels. The ChoiceType ensures that the ‘priority’ column can only have values chosen from a predefined set: ‘Low’, ‘Medium’, and ‘High’. The declarative base class, declarative_base(), is utilized to create the base for the model, and the table is named ‘tasks’.
Python3
from sqlalchemy import create_engine, Column, Integer, String from sqlalchemy.ext.declarative import declarative_base from sqlalchemy_utils import ChoiceType engine = create_engine( "sqlite:///:memory:" ) Base = declarative_base() class Task(Base): __tablename__ = 'tasks' id = Column(Integer, primary_key = True ) name = Column(String, nullable = False ) priority = Column(ChoiceType(choices = { 'low' : 'Low' , 'medium' : 'Medium' , 'high' : 'High' }), nullable = False ) Base.metadata.create_all(engine) |
Output:
Installing Sqlalchemy-Utils using Python Pip
In this article, we will explore how to Install Sqlalchemy-Utils Using Pip. To utilize Sqlalchemy-Utils functionalities, it is essential to Install Sqlalchemy-Utils Using Pip and this can be achieved by following the steps outlined below.