Skip to main content

SQLAlchemy core, but fancier

Project description


SQLAlchemy core, but fancier.

import sqlalchemy as sa

from sqla_fancy_core import TableFactory

tf = TableFactory()

# Define a table
class Author:

    id = tf.auto_id()
    name = tf.string("name")
    created_at = tf.created_at()
    updated_at = tf.updated_at()

    Table = tf("author")

# Define a table
class Book:

    id = tf.auto_id()
    title = tf.string("title")
    author_id = tf.foreign_key("author_id",
    created_at = tf.created_at()
    updated_at = tf.updated_at()

    Table = tf("book")

# Create the tables
engine = sa.create_engine("sqlite:///:memory:")

with engine.connect() as conn:
    # Insert author
    qry = (
        .values({ "John Doe"})
    author = next(conn.execute(qry))
    author_id = author._mapping[]
    assert author_id == 1

    # Insert book
    qry = (
        .values({Book.title: "My Book", Book.author_id: author_id})
    book = next(conn.execute(qry))
    assert book._mapping[] == 1

    # Query the data
    qry =, Book.title).join(
        Book.author_id ==,
    result = conn.execute(qry).fetchall()
    assert result == [("John Doe", "My Book")], result

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqla_fancy_core-0.3.0.tar.gz (5.2 kB view hashes)

Uploaded Source

Built Distribution

sqla_fancy_core-0.3.0-py3-none-any.whl (4.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page