SQLAlchemy Views

0.3.2 · active · verified Thu Apr 16

SQLAlchemy-views extends SQLAlchemy by providing `CreateView` and `DropView` constructs, allowing developers to manage database views within their Python applications using SQLAlchemy's DDL capabilities. The current version is 0.3.2. Releases are infrequent as the library's codebase is small and built upon stable components of the SQLAlchemy API, requiring minimal ongoing maintenance to maintain compatibility with new SQLAlchemy or Python versions. It supports both SQLAlchemy 1.x and 2.x.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a base table, then create and drop a database view using `sqlalchemy-views` with an in-memory SQLite database. It shows how to use `CreateView` with a `Table` object representing the view and a raw SQL `text` definition. It also includes an example of querying the created view.

import sqlalchemy as sa
from sqlalchemy import Table, Column, Integer, String, MetaData, text
from sqlalchemy.schema import CreateTable
from sqlalchemy_views import CreateView, DropView

# 1. Setup an in-memory SQLite database
engine = sa.create_engine('sqlite://', echo=True)
metadata = MetaData()

# 2. Define a base table
users_table = Table(
    'users',
    metadata,
    Column('id', Integer, primary_key=True),
    Column('name', String(50)),
    Column('email', String(100))
)

# 3. Define the view using a Table object and a SQL definition
active_users_view = Table('active_users', metadata, Column('id', Integer), Column('name', String), Column('email', String))
view_definition = text("SELECT id, name, email FROM users WHERE active = 1")

create_active_users_view = CreateView(active_users_view, view_definition)
drop_active_users_view = DropView(active_users_view)

# For demonstration, assume 'users' has an 'active' column for the view, 
# but for simplicity, the base table here doesn't have it to keep it minimal. 
# In a real app, users_table would also have 'active'.

with engine.connect() as connection:
    # Create the base table (users)
    metadata.create_all(connection)
    connection.execute(text("INSERT INTO users (id, name, email) VALUES (1, 'Alice', 'alice@example.com')"))
    connection.execute(text("INSERT INTO users (id, name, email) VALUES (2, 'Bob', 'bob@example.com')"))
    connection.execute(text("INSERT INTO users (id, name, email) VALUES (3, 'Charlie', 'charlie@example.com')"))
    connection.execute(text("ALTER TABLE users ADD COLUMN active BOOLEAN DEFAULT 1"))
    connection.execute(text("UPDATE users SET active = 0 WHERE id = 2"))
    
    # Apply the CreateView DDL
    connection.execute(create_active_users_view)
    print("\n--- View 'active_users' created ---")
    
    # Query the view
    result = connection.execute(active_users_view.select()).fetchall()
    print("Data from 'active_users' view:", result)

    # Apply the DropView DDL
    connection.execute(drop_active_users_view)
    print("\n--- View 'active_users' dropped ---")
    
    connection.commit()

view raw JSON →