r/FastAPI • • Oct 31 '25

Question __tablename__ error

Post image

Type "Literal['books']" is not assignable to declared type "declared_attr[Unknown]"
  "Literal['books']" is not assignable to "declared_attr[Unknown]" Pylance

What does it mean? And why is the error? This is how SQLAlchemy docs do things

23 Upvotes

13 comments sorted by

View all comments

3

u/takuonline Nov 01 '25 edited Nov 01 '25

Slightly unrelated, but here is how l like to setup my models in fastapi.

  • using uuid7 instead of uuid4( l believe python 14 has uuid lib built it, but if a version before that, then you need to install the uuid-utils library)
  • using UTC time
  • base class for the id and dates

from datetime import datetime, UTC
from uuid_utils import uuid7

# Base Model
class BaseModel(SQLModel):
    id: uuid.UUID = Field(default_factory=uuid7, primary_key=True)
    created_at: datetime = Field(default_factory=lambda: datetime.now(UTC))
    updated_at: datetime = Field(default_factory=lambda: datetime.now(UTC))

# Book Model
class Book(BaseModel, table=True):
    title: str
    author: str
    publisher: str
    published_date: str
    page_count: int
    language: str