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:
InterfaceError: (sqlite3.InterfaceError) Error binding parameter 1 - probably unsupported type.
[SQL: INSERT INTO block (id, "values") VALUES (?, ?)]
[parameters: (0, ['test', 'test2'])]
(Background on this error at: https://sqlalche.me/e/14/rvf5)
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.
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 more
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.
Reacted by Gustavo Soares, Emir Karamehmetoglu, Magnus Markling, eugene, Andrii Bovsunovskyi, Marek Dobransky, Joris Guerry, Matthieu LAURENT, BLTHDJ, Samo Kolter and 8 more
Reacted by Gustavo Soares, Matthew Aylward and Jonathan Vargas
Reacted by Gustavo Soares, voxofox and Pedro Xavier
Reacted by Abdulaziz Al-Homaid, gsouveton, Pedro Xavier, Rômulo Gadelha, Martin Thorsen Ranang and AzlanCoding
Reacted by Gustavo Soares, Jeremy T. Hetzel, Harpo, Brian Antonelli and michelclemer
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.
Why this is not supported by default?
I mean, is it possible for the user to accomplish the sames as OP wants but without using values: 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 Karamehmetoglu
This is probably not that high on the priority list as there are workarounds, but even just having a list column use a Column(JSON) under the hood and not needing arbitrary_types_allowed in 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 harder
@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 more
Reacted by Natalia Chodelski, Vinicius Aguiar, Data Engineering Funderz Group, Thomas Schauer-Köckeis, Fibert Loyee and Robert Harris
Activity
mkarbo commented on Dec 9, 2021
I used JSON type
with partial success as a workaround for a small project.
antont commented on Feb 18, 2022
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.
GussSoares commented on Feb 23, 2022
Thank you @antont !! perfectly work for postgres database
FilipeMarch commented on Mar 20, 2022
Why this is not supported by default?
I mean, is it possible for the user to accomplish the sames as OP wants but without using values: List[str], maybe representing it on another way that SQLModel allows
I ended up doing what @mkarbo suggested
antont commented on Mar 21, 2022
@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.
0dminnimda commented on Oct 22, 2022
Are there any updates on this?
srausser commented on Oct 25, 2023
Are there any updates on this?
Matthieu-LAURENT39 commented on Oct 25, 2023
This is probably not that high on the priority list as there are workarounds, but even just having a list column use a Column(JSON) under the hood and not needing arbitrary_types_allowed in 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 harder
barseghyanartur commented on Jan 16, 2024
I think the answer of mkarbo is just perfect.
JakNowy commented on Apr 12, 2024
It'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! :)
Nikola-Milovic commented on Oct 10, 2024
Any proposals open for this? Seems like a pretty common issue? Or is this a limitation on sqlalchemy side?
JesusMF23 commented on Mar 22, 2025
bump, we need this, is pretty common in today's use cases
enrh commented on May 5, 2025
Bump
pauldamian commented on May 19, 2025
Bump.
I'm getting ValueError: <class 'list'> has no matching SQLAlchemy type in sqlmodel's main module.
VGBijpuria commented on Jun 28, 2025
If this doesn't get added soon, I'll write a PR
wide0s commented on Aug 7, 2025
And I need it, for my use cases. Voted.