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
{{ message }}
Repository navigation
How can I change the string column type to Unicode? #156
In mssql, the names and json_data columns are made of varchar.
Since I want an nvarchar column, I specified type_ separately in the sa_column_kwargs option.
However, I confirmed that an error occurred in sqlalchemy.
This error seems to occur when the positional argumet and keyword argumet for type_ overlap. File "/usr/local/lib/python3.9/site-packages/sqlalchemy/sql/schema.py", line 1586, in __init__ raise exc.ArgumentError( sqlalchemy.exc.ArgumentError: May not pass type_ positionally and as a keyword.
In this case, I would appreciate it if you could tell me what to do.
Which currently doesn't support Unicode. One option would be to add Unicode to the options supported by get_sqlalchemy_type, or to deal with the special case of receiving a type_ kwarg.
IMO allowing advanced users to pass through whatever they want via sa_column_kwargs is a better/more generic approach. I'll do a PR for that 👍
Edit: whoops this just occurred to me when I used it. Easier way to do this is to set the column type directly via sa_column:
Thank you so much for letting me know a solution that I can use right away. Options such as max_length or nullable should be specified again in the sa_column, but it's not a big problem right now.
Thank you @RobertRosca! Thank you SQLModel!
So setting sa_column worked like you wanted? If so could you close the issue? I'll also look into doing a PR to add this use case to the docs, with Unicode as an example.
Activity
RobertRosca commentedon Nov 17, 2021
Looked into this a bit.
When you pass
sa_column_kwargsthose get propagated through to when the sa column is created here:https://github.com/tiangolo/sqlmodel/blob/02da85c9ec39b0ebb7ff91de3045e0aea28dc998/sqlmodel/main.py#L447
The issue happens since you're passing
type_ = Unicodeas a keyword argument, but SQLModel already passessa_typethrough as the positional argument.The
sa_typevariable is found by:https://github.com/tiangolo/sqlmodel/blob/02da85c9ec39b0ebb7ff91de3045e0aea28dc998/sqlmodel/main.py#L373-L409
Which currently doesn't support
Unicode. One option would be to addUnicodeto the options supported byget_sqlalchemy_type, or to deal with the special case of receiving atype_kwarg.IMO allowing advanced users to pass through whatever they want via
sa_column_kwargsis a better/more generic approach. I'll do a PR for that 👍Edit: whoops this just occurred to me when I used it. Easier way to do this is to set the column type directly via
sa_column:phi-friday commentedon Nov 18, 2021
Thank you so much for letting me know a solution that I can use right away. Options such as max_length or nullable should be specified again in the sa_column, but it's not a big problem right now.
Thank you @RobertRosca! Thank you SQLModel!
RobertRosca commentedon Nov 18, 2021
😁 glad to help!
So setting
sa_columnworked like you wanted? If so could you close the issue? I'll also look into doing a PR to add this use case to the docs, with Unicode as an example.phi-friday commentedon Nov 18, 2021
@RobertRosca
Of course!
Thank you!
tiangolo commentedon Oct 29, 2023
In SQLModel
0.0.11you can now setsa_typewith the SQLAlchemy type. 🎉fbuccioni commentedon Mar 21, 2026
I have done this by monkey patching:
Maybe it could be a global config system or flag to use Unicode in AutoStrings