PG Array in declarative mapping

3,589 views
Skip to first unread message

Samuel Killin

unread,
Feb 19, 2023, 7:21:20 PM2/19/23
to sqlalchemy
I'm struggling to define a SQLAlchemy column for a postgres string array using the new declarative mapping pattern in 2.0.

I would expect something like this to work:
my_col: Mapped[list[str]] = mapped_column()

However, this throws with:
sqlalchemy.exc.ArgumentError: Could not locate SQLAlchemy Core type for Python type list[str] inside the 'my_col' attribute Mapped annotation

Any ideas?

Mike Bayer

unread,
Feb 21, 2023, 9:51:07 AM2/21/23
to noreply-spamdigest via sqlalchemy
hey there -

a list[str] can be lots of different kinds of datatype, like JSON, custom array types, and PG ARRAY, which is specific to PostgreSQL.  so for non-obvious datatypes like this you indicate them explicitly:

from sqlalchemy.dialects.postgresql import ARRAY
from sqlalchemy import String
col: Mapped[list[str]] = mapped_column(ARRAY(String))


hoping this is clear from the doc at https://docs.sqlalchemy.org/en/20/orm/declarative_tables.html#using-annotated-declarative-table-type-annotated-forms-for-mapped-column which illustrates several of the mapped_column() with explicit datatype and also states "If the mapped_column() construct indicates an explicit type as passed to the mapped_column.__type argument, then the given Python type is disregarded".
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
 
 
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages