I am currently defining ORMs and DTOs in my fastapi application, and using SQLAlchemy 2.0 for this job.
Many sources, including the official docs, specify that the way to use mapped types with ORMs is to use mapped_columns and then specify the types along with Mapped, like the following:
class Base(MappedAsDataclass, DeclarativeBase):
pass
class Board(Base):
__tablename__ = "board"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
name: Mapped[str] = mapped_column(String)
However, upon doing this, pylance highlights the type of the mapped columns as MappedColumn[Any] instead of the implied types. My questions are,
Why does this happen? And is this intended behavior? From my understanding, types should map from mysql types to python types, but that doesn't seem to be the case here.
How do primary key defaults work? Specifically, for the code above, when I try to initialize a board instance with
b = Board(name='test'), pylance shows errorArgument missing for parameter "id". This seems strange as the autoincrement should be recognized as having a default value. Do I have to explicitly add adefault=parameter in the ORM definition to avoid this error?