-
-
Notifications
You must be signed in to change notification settings - Fork 783
Added typecheck to sqlalchemy JSON #931
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
In sqlmodel/main.py there was missing a typecheck for the JSON type supported by sqlalchemy. So it was not possible to define a model with a JSON field.
|
please add JSONB support too |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A single line change that is ready to be merged and doesn't require anything really.
|
Ah, never mind. Found a fix: from sqlmodel import SQLModel, Field, JSON
class User(SQLModel, table=True):
id: int = Field(..., primary_key=True)
data: JSON = Field(default={}, sa_type=JSON) |
|
Hi @vduseev @LucaGelmini is there an update on when this can be merged? |
|
As I see this can be merged right now. The JSON compatibility was supported by SQLAlchemy, just that it wasn't implemented inside the SQLModel map of the SQLAlchemy types. So, I just changed two lines to include the JSON class 😂. Very straightforward change. |
|
First collaboration in a repo though, so if I'm missing something please tell me 🫡 |
returning the sqlalchemy Uuid type
|
Really want this to be merged 🙏 |
| if issubclass(type_, uuid.UUID): | ||
| return Uuid | ||
| if issubclass(type_, JSON): | ||
| return JSON |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't you want issubclass(type_, Mapping) rather here?
Also it is probably not clear if JSON or JSONB is the desired type.
Maybe better to document the workaround in the docs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't it be if issubclass(type_, pydantic.Json): instead?
Or better
if issubclass(type_, (Sequence, Mapping, Json)):
return JSON
YuriiMotov
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@LucaGelmini, thanks for your interest!
Are you still ready to work on this?
We need to cover this by tests at least.
But I would actually extend this to also support Sequence and Mapping
| if issubclass(type_, uuid.UUID): | ||
| return Uuid | ||
| if issubclass(type_, JSON): | ||
| return JSON |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't it be if issubclass(type_, pydantic.Json): instead?
Or better
if issubclass(type_, (Sequence, Mapping, Json)):
return JSON|
This pull request has a merge conflict that needs to be resolved. |
|
As this PR has been waiting for the original user for a while but seems to be inactive, it's now going to be closed. But if there's anyone interested, feel free to create a new PR. |
|
Is anybody up to take this over? |
In sqlmodel/main.py there was missing a typecheck for the JSON type supported by sqlalchemy. So it was not possible to define a model with a JSON field.