You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I used the GitHub search to find a similar issue and didn't find it.
I searched the SQLModel documentation, with the integrated search.
I already searched in Google "How to X in SQLModel" and didn't find any information.
I already read and followed all the tutorial in the docs and didn't find an answer.
I already checked if it is not related to SQLModel but to Pydantic.
I already checked if it is not related to SQLModel but to SQLAlchemy.
Commit to Help
I commit to help with one of those options 👆
Example Code
importuuidfromtypingimportOptionalfromsqlmodelimportSQLModel, Fieldfromsqlalchemy.sql.expressionimportfuncclassEvent(SQLModel, table=True):
event_uuid: Optional[uuid.UUID] =Field(
primary_key=True, nullable=False, sa_column_kwargs=dict(server_default=func.gen_random_uuid())
)
# a PostgreSQL database with this model cannot have a migration created.
Description
Models which use sqlmodel.sql.sqltypes.GUID and a server_default cannot be upgraded by Alembic, using PostgreSQL
Problem
create a column with UUID and server default, e.g.
update the database. Use the database. All is fine.
Create another migration with alembic. The analysis and comparison of the current model with the current database will result in the following error:
sqlalchemy.exc.DataError: (psycopg2.errors.InvalidTextRepresentation) invalid input syntax for type uuid: "gen_random_uuid()"
LINE 1: SELECT gen_random_uuid() = 'gen_random_uuid()'
Workaround
Replacing the Field specification in the model with
there are also some other bugs when using sa_column_kwargs and alembic. it is better to use sa_column now
What bugs? I prefer not to use sa_column. Developers can accidentally do things like Field(sa_column=Column(JSON), nullable=False), where sa_column causes nullable to be ignored, and sa_column's default value of nullable=True is used instead. (0.0.11 added an error for this scenario.) That said, if there are worse issues with sa_column_kwargs, it'd be good to know.
First Check
Commit to Help
Example Code
Description
Models which use
sqlmodel.sql.sqltypes.GUID
and aserver_default
cannot be upgraded by Alembic, using PostgreSQLProblem
alembic
creates a table like this:Workaround
Replacing the
Field
specification in the model withgets rid of the error. But this bypasses SqlModel's type inference with a more lengthy and explicit
sa.Column
.It would appear that sqlmodel.sql.sqltypes as a type in a
sa.Column
somehow does not behave the same as the type class from sqlalchemy.Operating System
Linux
Operating System Details
No response
SQLModel Version
0.0.6
Python Version
3.10
Additional Context
alembic 1.8.1
sqlalchemy 1.4.35
The text was updated successfully, but these errors were encountered: