SQLAlchemy core, but fancier
Project description
sqla-fancy-core
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", Author.id)
created_at = tf.created_at()
updated_at = tf.updated_at()
Table = tf("book")
# Create the tables
engine = sa.create_engine("sqlite:///:memory:")
tf.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._mapping[Author.id]
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))
assert book._mapping[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.3.0.tar.gz
(5.2 kB
view details)
Built Distribution
File details
Details for the file sqla_fancy_core-0.3.0.tar.gz
.
File metadata
- Download URL: sqla_fancy_core-0.3.0.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8d2f39fc018c3e79ffbd45c0e50fbcc30a492866f575e5813980918608e27ac0 |
|
MD5 | eb069bbe5faf7d88ac7fd2561f689c7a |
|
BLAKE2b-256 | 2f4a764a35c12016e0b2aca2e299f6eb1e61517b205b754486b1992036ce0672 |
File details
Details for the file sqla_fancy_core-0.3.0-py3-none-any.whl
.
File metadata
- Download URL: sqla_fancy_core-0.3.0-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ca2ea01da30a560dc2bb8709a4b83c2772596ca89ab0c9b43cdb7d2cb5180234 |
|
MD5 | c86254b5c21458ad4a059b0c8724035b |
|
BLAKE2b-256 | 0d332e5e1629eff2fb5e4b816554f1105d35d216729bdf5600e5a6c84b95a611 |