Skip to content

Conversation

@LucaGelmini
Copy link

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.

LucaGelmini and others added 2 commits May 8, 2024 12:53
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.
@mmmcorpsvit
Copy link

please add JSONB support too

Copy link

@vduseev vduseev left a 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.

@vduseev
Copy link

vduseev commented Jul 12, 2024

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)

@alejsdev alejsdev added the feature New feature or request label Jul 12, 2024
@alessiamarcolini
Copy link

Hi @vduseev @LucaGelmini is there an update on when this can be merged?

@LucaGelmini
Copy link
Author

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.

@LucaGelmini
Copy link
Author

First collaboration in a repo though, so if I'm missing something please tell me 🫡

LucaGelmini

This comment was marked as outdated.

returning the sqlalchemy Uuid type
@TimNekk
Copy link

TimNekk commented Sep 6, 2024

Really want this to be merged 🙏

if issubclass(type_, uuid.UUID):
return Uuid
if issubclass(type_, JSON):
return JSON
Copy link

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

Copy link
Member

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

Copy link
Member

@YuriiMotov YuriiMotov left a 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
Copy link
Member

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

@github-actions github-actions bot added the conflicts Automatically generated when a PR has a merge conflict label Sep 5, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Sep 5, 2025

This pull request has a merge conflict that needs to be resolved.

@github-actions
Copy link
Contributor

github-actions bot commented Oct 6, 2025

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.

@github-actions github-actions bot closed this Oct 6, 2025
@YuriiMotov
Copy link
Member

Is anybody up to take this over?
We need to add tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

conflicts Automatically generated when a PR has a merge conflict feature New feature or request waiting

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants