Repository navigation
Add List type support #178
Description
Activity
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.
Reacted by Jing Wang, Frédéric Branchaud-Charron, 庭羲, Quinn Blenkinsop, Toni Alatalo, Tianle Chen, Antonio Perez, Jonathan Vargas, Stefan Uddenberg, codezilla451 and 24 moreWith 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]))
Reacted by Gustavo Soares, Emir Karamehmetoglu, Magnus Markling, eugene, Andrii Bovsunovskyi, Marek Dobransky, Joris Guerry, Matthieu LAURENT, BLTHDJ, Samo Kolter and 8 moreReacted by Gustavo Soares, Matthew Aylward and Jonathan VargasReacted by Gustavo Soares, voxofox and Pedro XavierReacted by Abdulaziz Al-Homaid, gsouveton, Pedro Xavier, Rômulo Gadelha, Martin Thorsen Ranang and AzlanCodingReacted by Gustavo Soares, Jeremy T. Hetzel, Harpo, Brian Antonelli and michelclemerWith 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]))
Thank you @antont !! perfectly work for postgres database
Why this is not supported by default?
I mean, is it possible for the user to accomplish the sames as OP wants but without usingvalues: List[str], maybe representing it on another way that SQLModel allows
I ended up doing what @mkarbo suggested@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.
Reacted by Filipe Marchesini and Emir KaramehmetogluAre there any updates on this?
Are there any updates on this?
Reacted by Matthieu LAURENT, Data Engineering Funderz Group, Anurag and VGBijpuriaMatthieu-LAURENT39 commented
on Oct 25, 2023 ContributorMore actionsThis is probably not that high on the priority list as there are workarounds, but even just having a
listcolumn use aColumn(JSON)under the hood and not needingarbitrary_types_allowedin the Pydantic config would be a massive upgrade.Although of course, it would be best to use
Column(ARRAY(...))when possible, but that's probably a bit harderReacted by Abdul RaufIt's not perfect, as it introduces code redundancy. Assume you have a model for Fastapi endpoint input validation:
class ItemBase(SQLModel): heroes: list[Hero] names: list[str] | None value: intThen you have to redeclare those in your actual SQLModel table:
class Item(ItemBase, table=True): heroes: list[Hero] = Relationship(back_populates="item") names: list[str] = Field(sa_column=Column(ARRAY(String), nullable=True))@tiangolo this would be alot cleaner to have it recognized out of the box! :)
Reacted by Matthieu LAURENT, TarmoYli, Armand Rego, Ilja Baroŭski, cdrcqnts, gavin, Vinicius Aguiar, Martin Thorsen Ranang, Nikola Milovic, Thomas Schauer-Köckeis and 8 moreReacted by Natalia Chodelski, Vinicius Aguiar, Data Engineering Funderz Group, Thomas Schauer-Köckeis, Fibert Loyee and Robert HarrisAny proposals open for this? Seems like a pretty common issue? Or is this a limitation on sqlalchemy side?
Reacted by Rodrigo, Anurag, Raoul Luqué, Jacob Windsor, La Min Ko, Robert Harris and Frαnçoisbump, we need this, is pretty common in today's use cases
Reacted by Muqsit, Robert Harris and FrαnçoisBump
Reacted by Luka Cerrutti, Tobi Lipede, VGBijpuria, savvaki and FrαnçoisReacted by Abdul RaufBump.
I'm gettingValueError: <class 'list'> has no matching SQLAlchemy typein sqlmodel's main module.If this doesn't get added soon, I'll write a PR
Reacted by Kovacs, GPla, Tobias Sette, colin-jensen, wide0s, VGBijpuria, JonathanJu, Dario Nascimento, Hugo, LeeJi Eun and 9 moreAnd I need it, for my use cases. Voted.
- locked and limited conversation to collaborators
on May 18, 2026
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