Replies: 17 comments 2 replies
-
|
I used JSON type from typing import List
from sqlmodel import Field, Session, SQLModel, create_engine, JSON, Column
class Block(SQLModel, table=True):
id: int = Field(..., primary_key=True)
values: List[str] = Field(sa_column=Column(JSON))
# Needed for Column(JSON)
class Config:
arbitrary_types_allowed = True
engine = create_engine("sqlite:///test_database.db", echo=True)
SQLModel.metadata.create_all(engine)
b = Block(id=0, values=['test', 'test2'])
with Session(engine) as session:
session.add(b)
session.commit()with partial success as a workaround for a small project. |
Beta Was this translation helpful? Give feedback.
-
|
With Postgres, you can use an array of e.g. strings or ints. I'm having it as a Set on Python side to verify that don't get duplicates, but List works too. I think they are not supported in Sqlite though. from sqlalchemy.dialects import postgresql #ARRAY contains requires dialect specific type
tags: Optional[Set[str]] = Field(default=None, sa_column=Column(postgresql.ARRAY(String())))
(...)
tagged = session.query(Item).filter(Item.tags.contains([tag])) |
Beta Was this translation helpful? Give feedback.
-
Thank you @antont !! perfectly work for postgres database |
Beta Was this translation helpful? Give feedback.
-
|
Why this is not supported by default? |
Beta Was this translation helpful? Give feedback.
-
|
@FilipeMarch - I guess one issue is that SQLite does not have arrays, whereas Postgres does. I'm using List but it means I can't use SQLite. Which is fine in our case, we need pg support only. |
Beta Was this translation helpful? Give feedback.
-
|
Are there any updates on this? |
Beta Was this translation helpful? Give feedback.
-
|
Are there any updates on this? |
Beta Was this translation helpful? Give feedback.
-
|
This is probably not that high on the priority list as there are workarounds, but even just having a Although of course, it would be best to use |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
|
It's not perfect, as it introduces code redundancy. Assume you have a model for Fastapi endpoint input validation: Then you have to redeclare those in your actual SQLModel table: @tiangolo this would be alot cleaner to have it recognized out of the box! :) |
Beta Was this translation helpful? Give feedback.
-
|
Any proposals open for this? Seems like a pretty common issue? Or is this a limitation on sqlalchemy side? |
Beta Was this translation helpful? Give feedback.
-
|
bump, we need this, is pretty common in today's use cases |
Beta Was this translation helpful? Give feedback.
-
|
Bump. |
Beta Was this translation helpful? Give feedback.
-
|
If this doesn't get added soon, I'll write a PR |
Beta Was this translation helpful? Give feedback.
-
|
And I need it, for my use cases. Voted. |
Beta Was this translation helpful? Give feedback.
-
|
@tiangolo Surprised by my feature request being converted to a discussion. Could you expand on that? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
I'm trying to store a list or similar types directly to the database.
Currently this does not seem to be the case. The above example code gives me the following error:
Wanted Solution
I would like to directly use the List type (or similar types like Dicts) to store data to a database column. I would expect SQLModel to serialize them.
Wanted Code
Alternatives
From another thread I tried to use this:
values: List[str] = Field(sa_column=Column(ARRAY(String)))
But this results in another error.
Operating System
Linux, Windows
Operating System Details
I'm working on the WSL.
SQLModel Version
0.0.4
Python Version
3.7.12
Additional Context
Beta Was this translation helpful? Give feedback.
All reactions