Install & Compatibility
Where this runs
tested against v0.8.8 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 1.214s · 46.8MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 4.2s · import 1.138s · 45MB
45MB installed
● package 45MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
PGView
✓ from alembic_utils.pg_view import PGView
PGFunction
✓ from alembic_utils.pg_function import PGFunction
PGMaterializedView
✓ from alembic_utils.pg_materialized_view import PGMaterializedView
PGTrigger
✓ from alembic_utils.pg_trigger import PGTrigger
PGPolicy
✓ from alembic_utils.pg_policy import PGPolicy
add_alembic_utils_meta
✓ from alembic_utils.utils import add_alembic_utils_meta
Primarily for older Alembic/Alembic-Utils versions; op.sync_alembic_utils() is preferred for newer setups.
This quickstart demonstrates how to define a PostgreSQL view and function using Alembic Utils, and how to use the `op.create_entity()`, `op.drop_entity()`, and `op.sync_alembic_utils()` methods within an Alembic migration script. The `MockOp` class allows the code to run and display the intended DDL operations without requiring an actual database connection or full Alembic project setup.
import sys
from alembic_utils.pg_view import PGView
from alembic_utils.pg_function import PGFunction
# Mock op object for demonstration purposes to show API usage.
# In a real Alembic migration, `op` is provided by Alembic.
class MockOp:
def execute(self, statement):
print(f"Executing DDL: {statement}")
def create_entity(self, entity):
print(f"Creating entity: {entity.entity_name}")
print(f" Definition: {entity.to_sql_statement()}")
def drop_entity(self, entity):
print(f"Dropping entity: {entity.entity_name}")
print(f" Definition: {entity.to_sql_drop_statement()}")
def sync_alembic_utils(self):
print("Synchronizing Alembic-Utils entities (mocked).")
op = MockOp()
# Define a PostgreSQL view
my_view = PGView(
schema="public",
signature="my_first_view",
definition="SELECT id, name FROM users WHERE active = TRUE;"
)
# Define a PostgreSQL function
my_function = PGFunction(
schema="public",
signature="my_first_function(a integer, b integer)",
definition="RETURNS integer LANGUAGE plpgsql IMMUTABLE AS $$ BEGIN RETURN a + b; END; $$",
returns="integer" # Required since 0.6.0
)
# Simulate usage in an Alembic migration's upgrade() function
print("--- Simulating 'upgrade' phase ---")
op.create_entity(my_view)
op.create_entity(my_function)
# For Alembic >= 1.10.0 and Alembic-Utils >= 0.8.0, use this in env.py
# to automatically discover and manage entities defined in your model.
# If you define entities directly in migration scripts, this might not be needed.
op.sync_alembic_utils()
print("\n--- Simulating 'downgrade' phase ---")
# Entities should be dropped in reverse order of creation if dependencies exist
op.drop_entity(my_function)
op.drop_entity(my_view)
Debug
Known issues
breaking`PGText` was removed in version `0.8.0`. Users who previously used `PGText` for defining functions or views must migrate their definitions to `PGFunction` or `PGView` respectively. This change provides more specific and structured interfaces for each entity type.fixReplace `PGText` definitions with `PGFunction` for functions or `PGView` for views, updating arguments as necessary (e.g., adding `returns` to `PGFunction`).
affects: >=0.8.0
breakingThe `PGFunction` constructor signature changed in version `0.6.0`, making the `returns` argument mandatory. Migrations using `PGFunction` from older versions will fail if `returns` is not provided.fixEnsure all `PGFunction` definitions include the `returns` argument specifying the return type (e.g., `returns='integer'` or `returns='void'`).
affects: >=0.6.0
gotchaAlembic integration for change detection and synchronization varies by version. For Alembic >= 1.10.0 and Alembic-Utils >= 0.8.0, the preferred method to automatically manage entities defined in your application models is `op.sync_alembic_utils()`, typically called in `env.py`. For older versions, `add_alembic_utils_meta(op)` was used in `env.py`.fixConsult the documentation for your specific Alembic and Alembic-Utils versions. Use `op.sync_alembic_utils()` if on modern versions, otherwise use `add_alembic_utils_meta(op)`.
affects: <0.8.0 (for `add_alembic_utils_meta`), >=0.8.0 (for `op.sync_alembic_utils`)
gotchaAlembic Utils is primarily designed for PostgreSQL. While some concepts might apply to other SQL dialects, the specific entity classes (e.g., `PGView`, `PGFunction`) and their underlying DDL generation are tailored to PostgreSQL syntax and features. Attempting to use these with other databases may lead to syntax errors or unexpected behavior.fixEnsure you are using Alembic Utils with a PostgreSQL database. For other databases, seek alternative migration tools for custom DDL.
affects: All versions
Errors
Common errors & fixes
ImportError: No module named 'app'
Alembic cannot locate the 'app' module because the current working directory is not set correctly.
fixSet the PYTHONPATH to the project's root directory before running Alembic commands: `PYTHONPATH=. alembic upgrade head`.
AttributeError: module 'alembic.context' has no attribute 'config'
The 'alembic.context' module is only available when the 'env.py' script is executed through the Alembic command within a directory containing an 'alembic.ini' file.
fixEnsure that you run Alembic commands from the directory containing the 'alembic.ini' file.
ImportError: cannot import name '_BindParamClause' from 'sqlalchemy.sql.expression'
This error occurs due to an incompatibility between Alembic and the installed version of SQLAlchemy.
fixUninstall and reinstall Alembic to ensure compatibility: `pip uninstall alembic` followed by `pip install alembic`.
AttributeError: 'int' object has no attribute '_compiler_dispatch'
This error arises when attempting to set a server default value as an integer instead of a string in Alembic migrations.
fixSpecify the server default as a string: `server_default='1'` instead of `server_default=1`.
AttributeError: 'str' object has no attribute '_autoincrement_column'
This error occurs when using 'op.bulk_insert' without defining a table object in Alembic migrations.
fixDefine the table object before performing bulk insert operations.
Upgrade
Version history
0.8.8latest on PyPI · released Apr 10, 2025
Audit
Dependencies
alembicrequiredAlembic Utils extends Alembic for database migrations.
SQLAlchemyoptionalUnderlying ORM framework that Alembic and Alembic Utils integrate with.