PostgreSQL ARRAY type. The :class:`_postgresql.ARRAY` type is constructed in the same way as the core :class:`_types.ARRAY` type; a member type is required, and a number of dimensions is recommended if the type is to be used for more than one dimension:: from sqlalchemy.dia
| 236 | |
| 237 | |
| 238 | class ARRAY(sqltypes.ARRAY[_T]): |
| 239 | """PostgreSQL ARRAY type. |
| 240 | |
| 241 | The :class:`_postgresql.ARRAY` type is constructed in the same way |
| 242 | as the core :class:`_types.ARRAY` type; a member type is required, and a |
| 243 | number of dimensions is recommended if the type is to be used for more |
| 244 | than one dimension:: |
| 245 | |
| 246 | from sqlalchemy.dialects import postgresql |
| 247 | |
| 248 | mytable = Table( |
| 249 | "mytable", |
| 250 | metadata, |
| 251 | Column("data", postgresql.ARRAY(Integer, dimensions=2)), |
| 252 | ) |
| 253 | |
| 254 | The :class:`_postgresql.ARRAY` type provides all operations defined on the |
| 255 | core :class:`_types.ARRAY` type, including support for "dimensions", |
| 256 | indexed access, and simple matching such as |
| 257 | :meth:`.types.ARRAY.Comparator.any` and |
| 258 | :meth:`.types.ARRAY.Comparator.all`. :class:`_postgresql.ARRAY` |
| 259 | class also |
| 260 | provides PostgreSQL-specific methods for containment operations, including |
| 261 | :meth:`.postgresql.ARRAY.Comparator.contains` |
| 262 | :meth:`.postgresql.ARRAY.Comparator.contained_by`, and |
| 263 | :meth:`.postgresql.ARRAY.Comparator.overlap`, e.g.:: |
| 264 | |
| 265 | mytable.c.data.contains([1, 2]) |
| 266 | |
| 267 | Indexed access is one-based by default, to match that of PostgreSQL; |
| 268 | for zero-based indexed access, set |
| 269 | :paramref:`_postgresql.ARRAY.zero_indexes`. |
| 270 | |
| 271 | Additionally, the :class:`_postgresql.ARRAY` |
| 272 | type does not work directly in |
| 273 | conjunction with the :class:`.ENUM` type. For a workaround, see the |
| 274 | special type at :ref:`postgresql_array_of_enum`. |
| 275 | |
| 276 | .. container:: topic |
| 277 | |
| 278 | **Detecting Changes in ARRAY columns when using the ORM** |
| 279 | |
| 280 | The :class:`_postgresql.ARRAY` type, when used with the SQLAlchemy ORM, |
| 281 | does not detect in-place mutations to the array. In order to detect |
| 282 | these, the :mod:`sqlalchemy.ext.mutable` extension must be used, using |
| 283 | the :class:`.MutableList` class:: |
| 284 | |
| 285 | from sqlalchemy.dialects.postgresql import ARRAY |
| 286 | from sqlalchemy.ext.mutable import MutableList |
| 287 | |
| 288 | |
| 289 | class SomeOrmClass(Base): |
| 290 | # ... |
| 291 | |
| 292 | data = Column(MutableList.as_mutable(ARRAY(Integer))) |
| 293 | |
| 294 | This extension will allow "in-place" changes such to the array |
| 295 | such as ``.append()`` to produce events which will be detected by the |
no outgoing calls