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

Fix sqlalchemy v2 compability #22

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
26 changes: 18 additions & 8 deletions fastapi_users_db_sqlmodel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

from fastapi_users.db.base import BaseUserDatabase
from fastapi_users.models import ID, OAP, UP
from pydantic import UUID4, EmailStr
from pydantic import UUID4, EmailStr, ConfigDict
from pydantic.version import VERSION as PYDANTIC_VERSION
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import selectinload
from sqlmodel import Field, Session, SQLModel, func, select
from sqlmodel import Field, Session, SQLModel, func, select, AutoString

__version__ = "0.3.0"

PYDANTIC_V2 = PYDANTIC_VERSION.startswith("2.")

class SQLModelBaseUserDB(SQLModel):
__tablename__ = "user"
Expand All @@ -20,16 +21,21 @@ class SQLModelBaseUserDB(SQLModel):
email: str
else:
email: EmailStr = Field(
sa_column_kwargs={"unique": True, "index": True}, nullable=False
sa_column_kwargs={"unique": True, "index": True}, nullable=False,
sa_type=AutoString
)
hashed_password: str

is_active: bool = Field(True, nullable=False)
is_superuser: bool = Field(False, nullable=False)
is_verified: bool = Field(False, nullable=False)

class Config:
orm_mode = True
if PYDANTIC_V2: # pragma: no cover
model_config = ConfigDict(from_attributes=True) # type: ignore
else: # pragma: no cover

class Config:
orm_mode = True


class SQLModelBaseOAuthAccount(SQLModel):
Expand All @@ -44,8 +50,12 @@ class SQLModelBaseOAuthAccount(SQLModel):
account_id: str = Field(index=True, nullable=False)
account_email: str = Field(nullable=False)

class Config:
orm_mode = True
if PYDANTIC_V2:
model_config = ConfigDict(from_attributes=True) # type: ignore
else:

class Config:
orm_mode = True


class SQLModelUserDatabase(Generic[UP, ID], BaseUserDatabase[UP, ID]):
Expand Down
12 changes: 8 additions & 4 deletions fastapi_users_db_sqlmodel/access_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
from typing import Any, Dict, Generic, Optional, Type

from fastapi_users.authentication.strategy.db import AP, AccessTokenDatabase
from pydantic import UUID4
from pydantic import UUID4, ConfigDict
from pydantic.version import VERSION as PYDANTIC_VERSION
from sqlalchemy import Column, types
from sqlalchemy.ext.asyncio import AsyncSession
from sqlmodel import Field, Session, SQLModel, select

from fastapi_users_db_sqlmodel.generics import TIMESTAMPAware, now_utc


PYDANTIC_V2 = PYDANTIC_VERSION.startswith("2.")
class SQLModelBaseAccessToken(SQLModel):
__tablename__ = "accesstoken"

Expand All @@ -24,8 +25,11 @@ class SQLModelBaseAccessToken(SQLModel):
)
user_id: UUID4 = Field(foreign_key="user.id", nullable=False)

class Config:
orm_mode = True
if PYDANTIC_V2: # pragma: no cover
model_config = ConfigDict(from_attributes=True) # type: ignore
else: # pragma: no cover
class Config:
orm_mode = True


class SQLModelAccessTokenDatabase(Generic[AP], AccessTokenDatabase[AP]):
Expand Down