Skip to content

Commit

Permalink
Merge pull request #300 from lsst-sqre/tickets/DM-46034a
Browse files Browse the repository at this point in the history
DM-46034: Remove remaining declarative_base references
  • Loading branch information
rra committed Sep 16, 2024
2 parents 4b62ef6 + 7301ced commit 3423ebf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
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

0 comments on commit 3423ebf

Please sign in to comment.