Skip to content

SAWarning: Unmanaged access of declarative attribute user_id from non-mapped class SQLAlchemyBaseAccessTokenTableUUID #1490

@andrei-samofalov

Description

@andrei-samofalov

Hi! Thanks for the library!

I have some interesting bug with overriden SQLAlchemyBaseAccessTokenTable.
For my implementation it was necessary to overwrite the name of the User table, so my code looks like

class JWTToken(SQLAlchemyBaseAccessTokenTable[int], Base):
    ...
    user_id: Mapped[int] = mapped_column(
        Integer,
        ForeignKey("app_users_user.id", ondelete="cascade"),
        nullable=False,
    )

Note that I use the SQLAlchemyBaseAccessTokenTable[int] inheritance, not SQLAlchemyBaseAccessTokenTableUUID

on app startup i see this warning

/usr/lib/python3.11/inspect.py:573: SAWarning: Unmanaged access of declarative attribute user_id from non-mapped class SQLAlchemyBaseAccessTokenTableUUID

Obviosly this warning is not expected.

Configuration

  • Python version : 3.11
  • FastAPI version : 0.115.6
  • FastAPI Users version : 14.0.0

Suggestions

Neither SQLAlchemyBaseAccessTokenTable nor SQLAlchemyBaseAccessTokenTableUUID has sqlalchemy declarative meta cofigured, they are just mixins.
I don't really know why declared_attr decorator is used, but if it is possible it can be replaced by plain mapped attr like:

class SQLAlchemyBaseAccessTokenTable(Generic[ID]):
    """Base SQLAlchemy access token table definition."""

    __tablename__ = "accesstoken"
    __user_tablename_id__ = 'user.id'

    if TYPE_CHECKING:  # pragma: no cover
        token: str
        created_at: datetime
        user_id: ID
    else:
        token: Mapped[str] = mapped_column(String(length=43), primary_key=True)
        created_at: Mapped[datetime] = mapped_column(
            TIMESTAMPAware(timezone=True), index=True, nullable=False, default=now_utc
        )
        user_id: Mapped[ID] = mapped_column(
            ForeignKey(__user_tablename_id__, ondelete="cascade"),
            nullable=False
        )

also hardcoded user tablename can be replaced by the class attr, f.e. __user__tablename__ (or __user_tablename_id__ for direct fk link) so it would be easier to configure inherited classes

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions