Skip to content

SQLAlchemy Models

All predefined models are located in fastauth.contrib.sqlalchemy.models module.

User model

The User model represents the user data in the database. It is the most important model in the application. To define simple User model you can use SQLAlchemyBaseUserModel class.

from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
from fastauth.contrib.sqlalchemy import models


class Model(DeclarativeBase):
    pass


class User(models.SQLAlchemyBaseUserUUID, Model):
    pass

User ID type

By default SQLAlchemyBaseUserUUID use UUID type as primary key. If you want to use another type, you can inherit SQLAlchemyBaseUser class and override id field.:

class User(models.SQLAlchemyBaseUser[int], Model):
    id: Mapped[int] = mapped_column(autoincrement=True, primary_key=True)

Role and Permission models

Role and Permission models are used to define the user roles and permissions. They are used to define the user access rights. To define simple Role and Permission models you can use SQLAlchemyBaseRoleModel and SQLAlchemyBasePermissionModel classes. Also we need create many-to-many relationship between User and Role models and between Role and Permission models. Inherit SQLAlchemyBaseRolePermissionRel class and set Mapped relationship in RoleModel.

from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
from fastauth.contrib.sqlalchemy import models


class Model(DeclarativeBase):
    pass


class Role(models.SQLAlchemyBaseRole, Model):
    permissions: Mapped[list["Permission"]] = relationship(
        secondary="role_permission_rel", lazy="joined"
    )


class Permission(models.SQLAlchemyBasePermission, Model):
    pass


class RolePermissionRel(models.SQLAlchemyBaseRolePermissionRel, Model):
    pass

And after that we need to bind our Role and Permission models with User model. FastAuth provide UserRBACMixin. User also have many-to-many relationship with permissions and role. We need to add class with inherited SQLAlchemyBaseUserPermissionRel class

from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
from fastauth.contrib.sqlalchemy import models


class Model(DeclarativeBase):
    pass


class Role(models.SQLAlchemyBaseRole, Model):
    permissions: Mapped[list["Permission"]] = relationship(
        secondary="role_permission_rel", lazy="joined"
    )


class Permission(models.SQLAlchemyBasePermission, Model):
    pass


class RolePermissionRel(models.SQLAlchemyBaseRolePermissionRel, Model):
    pass


class User(models.SQLAlchemyBaseUserUUID, models.UserRBACMixin, Model):
    role: Mapped[Role] = relationship(lazy="joined")
    permissions: Mapped[list[Permission]] = relationship(
        secondary="user_permission_rel", lazy="joined"
    )


class UserPermissionRel(models.SQLAlchemyBaseUserPermissionRel, Model):
    pass

OAuth model

OAuth model is used to store the OAuth tokens. It is used to authenticate the user with OAuth providers. To define simple OAuth model you can use SQLAlchemyBaseOAuthAccountUUID class.

from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
from fastauth.contrib.sqlalchemy import models


class Model(DeclarativeBase):
    pass


class OAuthAccount(models.SQLAlchemyBaseOAuthAccountUUID, Model):
    pass

OAuth ID type

By default SQLAlchemyBaseOAuthAccountUUID use UUID type as primary key. If you want to use another type, you can inherit SQLAlchemyBaseOAuthAccount class and override id field.:

class OAuthAccount(models.SQLAlchemyBaseOAuthAccount[int], Model):
    id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)

Then we need to bind our OAuth model with User model. FastAuth provide UserOAuthMixin.

from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
from fastauth.contrib.sqlalchemy import models


class Model(DeclarativeBase):
    pass


class OAuthAccount(models.SQLAlchemyBaseOAuthAccountUUID, Model):
    pass


class User(models.SQLAlchemyBaseUserUUID, models.UserOAuthMixin, Model):
    oauth_accounts: Mapped[list[OAuthAccount]] = relationship(lazy="joined")


class OAuthAccount(models.SQLAlchemyBaseOAuthAccount[int], Model):
    id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)

Full Example

To get full featured User model you can combine all user mixins

from sqlalchemy.orm import DeclarativeBase, Mapped, relationship
from fastauth.contrib.sqlalchemy import models


class Model(DeclarativeBase):
    pass


class Permission(models.SQLAlchemyBasePermission, Model):
    pass


class Role(models.SQLAlchemyBaseRole, Model):
    permissions: Mapped[list[Permission]] = relationship(
        secondary="role_permission_rel", lazy="joined"
    )


class OAuthAccount(models.SQLAlchemyBaseOAuthAccountUUID, Model):
    pass


class User(
    models.SQLAlchemyBaseUserUUID, models.UserRBACMixin, models.UserOAuthMixin, Model
):
    role: Mapped[Role] = relationship(lazy="joined")
    permissions: Mapped[list[Permission]] = relationship(
        secondary="user_permission_rel", lazy="joined"
    )
    oauth_accounts: Mapped[list[OAuthAccount]] = relationship(lazy="joined")


class UserPermissionRel(models.SQLAlchemyBaseUserPermissionRel, Model):
    pass


class RolePermissionRel(models.SQLAlchemyBaseRolePermissionRel, Model):
    pass