SQLAlchemy-Utils is a library that provides various utility functions, new data types, and helpers for SQLAlchemy. It extends SQLAlchemy's functionality by offering additional features to simplify common database tasks like managing database existence, custom column types (e.g., ChoiceType, UUIDType, EmailType), and ORM helpers. The library is actively maintained with frequent updates, typically a few minor releases per year, and is currently at version 0.42.1.
pip install sqlalchemy-utilsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `sqlalchemy-utils` to create and manage a PostgreSQL database programmatically, define a model with a custom `ChoiceType`, and perform basic ORM operations. It utilizes `database_exists`, `create_database`, and `ChoiceType` from the utility library. Environment variables are used for database credentials for security and flexibility. To run this, ensure a PostgreSQL server is accessible and you have `psycopg2-binary` or `psycopg` installed.
Upgrade Python to 3.9+ or pin `sqlalchemy-utils` version: `pip install 'sqlalchemy-utils<0.42.0'`.
Upgrade SQLAlchemy to 1.4.x or 2.x: `pip install 'sqlalchemy>=1.4,<3.0'`.
Upgrade to `sqlalchemy-utils>=0.39.0` or avoid installing `colour-science` alongside older versions.
Ensure the provided URL has credentials for a database with sufficient privileges to create new databases (e.g., `postgres` user for PostgreSQL) and that the database server is running.
Upgrade to `sqlalchemy-utils>=0.42.1`.
Install the appropriate database driver for your chosen backend, e.g., `pip install psycopg2-binary` for PostgreSQL or `pip install mysqlclient` for MySQL.
Install the appropriate database driver for your chosen SQLAlchemy dialect. For PostgreSQL, run: `pip install psycopg2-binary`. For MySQL, run: `pip install pymysql`.