Closed knkski closed 2 months ago
hi -
this doesnt look like a bug. If you have:
# Example SQLAlchemy table definition
class Example(Base):
__tablename__ = "example"
text: Mapped[str]
that's the same as:
# Example SQLAlchemy table definition
class Example(Base):
__tablename__ = "example"
text = Column(String())
String is VARCHAR. If your PG database has TEXT in it, that suggests that this model was not used to generate the model.
What I would recommend here is, well it depends what you want:
Mapped[str]
as TEXT at least for PostgreSQL:
class Base(DeclarativeBase):
type_annotation_map = {
str: String().with_variant(TEXT, "postgresql"),
}
Describe the bug
I upgraded Alembic from 1.11.x to 1.13.x. Everything mostly works, except now I'm getting unnecessary conversions of my
Mapped[str]
columns (in PostgreSQL). Alembic wants to convert them fromsa.TEXT
tosa.String
. This is likely due to the defaulting of compare_type to True with 1.12.0.Expected behavior
I expected Alembic to see that the columns are
text
, and are defined in code asMapped[str]
, and realize that everything's good. I believe that theMapped[str]
columns got created astext
because they were originally defined asMapped[str] = mapped_column(TEXT)
, then the= mapped_column(TEXT)
bit got removed as unnecessary, which turns out to be only true when Alembic isn't comparing types.To Reproduce
Error
Versions.
Additional context
This might not be a bug, and just working as intended. Looking at PostgreSQL's documentation, it says:
It makes sense to me for Alembic to default to
text
forMapped[str]
when using PostgreSQL, or at least not complain when seeing atext
column that corresponds toMapped[str]
in code. However, sincetext
isn't actually in the SQL standard, that might be too specialized and not something that Alembic wants to support. It would just be nice if I didn't have to add= mapped_column(TEXT)
everywhere.Have a nice day!