Skip to main content
deleted 18 characters in body; edited tags
Source Link
InSync
  • 12.2k
  • 5
  • 22
  • 60

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,

  1. 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.

  2. 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 error Argument missing for parameter "id". This seems strange as the autoincrement should be recognized as having a default value. Do I have to explicitly add a default= parameter in the ORM definition to avoid this error?

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,

  1. 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.

  2. 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 error Argument missing for parameter "id". This seems strange as the autoincrement should be recognized as having a default value. Do I have to explicitly add a default= parameter in the ORM definition to avoid this error?

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,

  1. 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.

  2. 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 error Argument missing for parameter "id". This seems strange as the autoincrement should be recognized as having a default value. Do I have to explicitly add a default= parameter in the ORM definition to avoid this error?

Spell out the prod nam so peop can rea wha it sa
Source Link
J_H
  • 21.2k
  • 5
  • 29
  • 50

I am currently defining ORMs and DTOs in my fastapi application, and using SQLASQLAlchemy 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,

  1. 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.

  2. 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 error Argument missing for parameter "id". This seems strange as the autoincrement should be recognized as having a default value. Do I have to explicitly add a default= parameter in the ORM definition to avoid this error?

I am currently defining ORMs and DTOs in my fastapi application, and using SQLA 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,

  1. 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.

  2. 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 error Argument missing for parameter "id". This seems strange as the autoincrement should be recognized as having a default value. Do I have to explicitly add a default= parameter in the ORM definition to avoid this error?

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,

  1. 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.

  2. 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 error Argument missing for parameter "id". This seems strange as the autoincrement should be recognized as having a default value. Do I have to explicitly add a default= parameter in the ORM definition to avoid this error?

Source Link
xxixx
  • 11
  • 2

Sqlalchemy mapped_column typing issues with pylance

I am currently defining ORMs and DTOs in my fastapi application, and using SQLA 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,

  1. 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.

  2. 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 error Argument missing for parameter "id". This seems strange as the autoincrement should be recognized as having a default value. Do I have to explicitly add a default= parameter in the ORM definition to avoid this error?