Skip to main content

Quart-SQLAlchemy

Quart-SQLAlchemy provides a simple wrapper for SQLAlchemy made for humans. I’ve kept things as simple as possible, abstracted much complexity, and implemented everything using the current best practices recommended by the SQLAlchemy developers and targets version 2.0.x+. As a convenience, a framework adapter is provided for Quart, but the rest of this library is framework agnostic.

The bundled SQLAlchemy object intentionally discards the use of scoped_session and it’s async counterpart. With version 2.x+, it’s expected that sessions are short lived and vanilla and context managers are used for managing sesssion lifecycle. Any operations that intend to change state should open an explicit transaction using the context manager returned by session.begin(). This pattern of usage prevents problems like sessions being shared between processes, threads, or tasks entirely, as opposed to the past conventions of mitigating this type of sharing. Another best practice is expecting any transaction to intermittently fail, and structuring your logic to automatically perform retries. You can find the retrying session context managers in the retry module.

Installing

Install and update using pip:

$ pip install quart-sqlalchemy

Install the latest release with unreleased pytest-asyncio fixes:

$ pip install git+ssh://git@github.com/joeblackwaslike/quart-sqlalchemy.git#egg=quart_sqlalchemy

Install a wheel from our releases:

$ pip install https://github.com/joeblackwaslike/quart-sqlalchemy/releases/download/v3.0.1/quart_sqlalchemy-3.0.1-py3-none-any.whl

Add to requirements.txt:

quart-sqlalchemy @ https://github.com/joeblackwaslike/quart-sqlalchemy/releases/download/v3.0.1/quart_sqlalchemy-3.0.1-py3-none-any.whl

A Simple Example

import sqlalchemy as sa
import sqlalchemy.orm
from sqlalchemy.orm import Mapped, mapped_column
from quart import Quart

from quart_sqlalchemy import SQLAlchemyConfig
from quart_sqlalchemy.framework import QuartSQLAlchemy

app = Quart(__name__)

db = QuartSQLAlchemy(
  config=SQLAlchemyConfig
      binds=dict(
          default=dict(
              engine=dict(
                  url="sqlite:///",
                  echo=True,
                  connect_args=dict(check_same_thread=False),
              ),
              session=dict(
                  expire_on_commit=False,
              ),
          )
      )
  ),
  app,
)

class User(db.Model)
    __tablename__ = "user"

    id: Mapped[int] = mapped_column(sa.Identity(), primary_key=True, autoincrement=True)
    name: Mapped[str] = mapped_column(default="default")

db.create_all()

with db.bind.Session() as s:
    with s.begin():
        user = User(username="example")
        s.add(user)
        s.flush()
        s.refresh(user)

    users = s.scalars(sa.select(User)).all()

print(user, users)
assert user in users

Contributing

For guidance on setting up a development environment and how to make a contribution to Quart-SQLAlchemy, see the contributing guidelines.

Metadata

Release files for quart-sqlalchemy 3.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for quart-sqlalchemy 3.0.4
File Size Uploaded
quart_sqlalchemy-3.0.4.tar.gz 2.1 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for quart-sqlalchemy 3.0.4
File Interpreter ABI Platform
quart_sqlalchemy-3.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 5.1 MB

Release files / quart_sqlalchemy-3.0.4.tar.gz

Download URL quart_sqlalchemy-3.0.4.tar.gz
Size 2.1 MB
Tags Source
SHA-256 checksum
How to use checksums
302f9da74ddd86fc02cf83d5a48d608796cd728e3c6892d98f992e0d49da0121
BLAKE2b-256 checksum
How to use checksums
4e77d3764f7c4d0a5abe692db68cbf0992a5766fe41c7b85ffa9af54a9bd1c52
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.10.1 CPython/3.11.5

Release files / quart_sqlalchemy-3.0.4-py3-none-any.whl

Download URL quart_sqlalchemy-3.0.4-py3-none-any.whl
Size 3.0 MB
Tags Python 3
SHA-256 checksum
How to use checksums
d262542fd83780917bd30bf7b6f33b291784a8a796e9962cf87c5aff7368c110
BLAKE2b-256 checksum
How to use checksums
e9d2f4a6711608b666b7538150ab4569c0058fbfdc068e605a918719791e9932
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.10.1 CPython/3.11.5

Release history Release notifications | RSS feed

This release

3.0.4 This release

2 release files

3.0.3

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.4.47

2 release files

1.4.46

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page