Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-46034: Remove remaining declarative_base references #300

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions docs/user-guide/database/initialize.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ In the file :file:`schema/base.py`, define the SQLAlchemy declarative base:

.. code-block:: python

from sqlalchemy.orm import declarative_base
from sqlalchemy.orm import DeclarativeBase


Base = declarative_base()
class Base(DeclarativeBase):
"""Declarative base for SQLAlchemy ORM model of database schema."""

In other files in that directory, define the database tables using the normal SQLAlchemy ORM syntax, one table per file.
In other files in that directory, define the database tables using the `normal SQLAlchemy ORM syntax <https://docs.sqlalchemy.org/en/20/orm/mapping_styles.html#declarative-mapping>`__, one table per file.
Each database table definition must inherit from ``Base``, imported from ``.base``.

In :file:`schema/__init__.py`, import the table definitions from all of the files in the directory, as well as the ``Base`` variable, and export them using ``__all__``.
In :file:`schema/__init__.py`, import the table definitions from all of the files in the directory, as well as the ``Base`` class, and export them using ``__all__``.

Using non-default PostgreSQL schemas
------------------------------------
Expand Down
10 changes: 6 additions & 4 deletions safir/tests/dependencies/db_session_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import structlog
from fastapi import Depends, FastAPI
from httpx import ASGITransport, AsyncClient
from sqlalchemy import Column, String
from sqlalchemy import String
from sqlalchemy.ext.asyncio import async_scoped_session
from sqlalchemy.future import select
from sqlalchemy.orm import declarative_base
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column

from safir.database import (
create_async_session,
Expand All @@ -20,15 +20,17 @@
)
from safir.dependencies.db_session import db_session_dependency

Base = declarative_base()

class Base(DeclarativeBase):
"""Declarative base for testing."""


class User(Base):
"""Tiny database table for testing."""

__tablename__ = "user"

username: str = Column(String(64), primary_key=True)
username: Mapped[str] = mapped_column(String(64), primary_key=True)


@pytest.mark.asyncio
Expand Down