{"library":"sqlalchemy-views","title":"SQLAlchemy Views","type":"library","description":"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.","language":"python","status":"active","last_verified":"Thu Apr 16","install":{"commands":["pip install sqlalchemy-views"],"cli":null},"imports":["from sqlalchemy_views import CreateView","from sqlalchemy_views import DropView","from sqlalchemy import Table","from sqlalchemy import MetaData","from sqlalchemy import text"],"auth":{"required":false,"env_vars":[]},"links":{"homepage":null,"github":"https://github.com/jklukas/sqlalchemy-views","docs":null,"changelog":null,"pypi":"https://pypi.org/project/sqlalchemy-views/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null},"quickstart":{"code":"import sqlalchemy as sa\nfrom sqlalchemy import Table, Column, Integer, String, MetaData, text\nfrom sqlalchemy.schema import CreateTable\nfrom sqlalchemy_views import CreateView, DropView\n\n# 1. Setup an in-memory SQLite database\nengine = sa.create_engine('sqlite://', echo=True)\nmetadata = MetaData()\n\n# 2. Define a base table\nusers_table = Table(\n    'users',\n    metadata,\n    Column('id', Integer, primary_key=True),\n    Column('name', String(50)),\n    Column('email', String(100))\n)\n\n# 3. Define the view using a Table object and a SQL definition\nactive_users_view = Table('active_users', metadata, Column('id', Integer), Column('name', String), Column('email', String))\nview_definition = text(\"SELECT id, name, email FROM users WHERE active = 1\")\n\ncreate_active_users_view = CreateView(active_users_view, view_definition)\ndrop_active_users_view = DropView(active_users_view)\n\n# For demonstration, assume 'users' has an 'active' column for the view, \n# but for simplicity, the base table here doesn't have it to keep it minimal. \n# In a real app, users_table would also have 'active'.\n\nwith engine.connect() as connection:\n    # Create the base table (users)\n    metadata.create_all(connection)\n    connection.execute(text(\"INSERT INTO users (id, name, email) VALUES (1, 'Alice', 'alice@example.com')\"))\n    connection.execute(text(\"INSERT INTO users (id, name, email) VALUES (2, 'Bob', 'bob@example.com')\"))\n    connection.execute(text(\"INSERT INTO users (id, name, email) VALUES (3, 'Charlie', 'charlie@example.com')\"))\n    connection.execute(text(\"ALTER TABLE users ADD COLUMN active BOOLEAN DEFAULT 1\"))\n    connection.execute(text(\"UPDATE users SET active = 0 WHERE id = 2\"))\n    \n    # Apply the CreateView DDL\n    connection.execute(create_active_users_view)\n    print(\"\\n--- View 'active_users' created ---\")\n    \n    # Query the view\n    result = connection.execute(active_users_view.select()).fetchall()\n    print(\"Data from 'active_users' view:\", result)\n\n    # Apply the DropView DDL\n    connection.execute(drop_active_users_view)\n    print(\"\\n--- View 'active_users' dropped ---\")\n    \n    connection.commit()\n","lang":"python","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}