SQLAlchemy core, but fancier
Project description
sqla-fancy-core
SQLAlchemy core, but fancier.
import sqlalchemy as sa
from sqla_fancy_core import TableFactory
metadata = sa.MetaData()
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", metadata)
# Define a table
class Book:
id = tf.auto_id()
title = tf.string("title")
author_id = tf.foreign_key("author_id", Author.id)
created_at = tf.created_at()
updated_at = tf.updated_at()
Table = tf("book", metadata)
# Create the tables
engine = sa.create_engine("sqlite:///:memory:")
metadata.create_all(engine)
with engine.connect() as conn:
# Insert author
qry = (
sa.insert(Author.Table)
.values({Author.name: "John Doe"})
.returning(Author.id)
)
author = next(conn.execute(qry))
(author_id,) = author
assert author_id == 1
# Insert book
qry = (
sa.insert(Book.Table)
.values({Book.title: "My Book", Book.author_id: author_id})
.returning(Book.id)
)
book = next(conn.execute(qry))
(book_id,) = book
assert book_id == 1
# Query the data
qry = sa.select(Author.name, Book.title).join(
Book.Table,
Book.author_id == Author.id,
)
result = conn.execute(qry).fetchall()
assert result == [("John Doe", "My Book")], result
Project details
Release history Release notifications | RSS feed
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.2.0.tar.gz
(3.3 kB
view details)
Built Distribution
File details
Details for the file sqla_fancy_core-0.2.0.tar.gz
.
File metadata
- Download URL: sqla_fancy_core-0.2.0.tar.gz
- Upload date:
- Size: 3.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.2 CPython/3.10.9 Linux/5.15.94
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6a8c65a354750099b9e5dc59a4a249b883810cf1bd8d73d4c71381686bf8b117 |
|
MD5 | a26121200ba35a35e88f8beed3e0f34f |
|
BLAKE2b-256 | 157acc02299d29f5bae3c3011b093bfa0dd0ff5c139a781c63cdc9083eb28f85 |
Provenance
File details
Details for the file sqla_fancy_core-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: sqla_fancy_core-0.2.0-py3-none-any.whl
- Upload date:
- Size: 3.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.2 CPython/3.10.9 Linux/5.15.94
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 61b4bcb6221c0304aecef7c84c83f98ec62ec24fbf6a20df1d4f463015823cc6 |
|
MD5 | 54a90acd32006d744cabe497d8c4dfdb |
|
BLAKE2b-256 | 056997e982e61bcdfb570ba04b0265d59ac558d55ecb1ae6cadc8b7dc6fcb2b1 |