summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/sql
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2022-08-30 09:50:03 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2022-08-30 09:50:03 -0400
commit2f146b172ad228e40f1e8d5f1d2abc888ae5e669 (patch)
treec76594ea127229899fdbf60618f96738878cc5ff /lib/sqlalchemy/sql
parent6da8d32fb1a5e706401bf0f11bb638ff94bd1bf4 (diff)
downloadsqlalchemy-2f146b172ad228e40f1e8d5f1d2abc888ae5e669.tar.gz
apply consistent ORM mutable notes for all mutable SQL types
in https://github.com/sqlalchemy/sqlalchemy/discussions/8447 I was surprised that we didnt have any notes about using Mutable for ARRAY classes, since we have them for HSTORE and JSON. Add a consistent topic box for these so we have something to point towards. Change-Id: Idfa1b2cbee67024545f4fa299e4c875075ec7d3f
Diffstat (limited to 'lib/sqlalchemy/sql')
-rw-r--r--lib/sqlalchemy/sql/sqltypes.py31
1 files changed, 30 insertions, 1 deletions
diff --git a/lib/sqlalchemy/sql/sqltypes.py b/lib/sqlalchemy/sql/sqltypes.py
index 99afabc61..fd52ec6ea 100644
--- a/lib/sqlalchemy/sql/sqltypes.py
+++ b/lib/sqlalchemy/sql/sqltypes.py
@@ -2204,11 +2204,15 @@ class JSON(Indexable, TypeEngine[Any]):
The :class:`_types.JSON` type, when used with the SQLAlchemy ORM, does not
detect in-place mutations to the structure. In order to detect these, the
- :mod:`sqlalchemy.ext.mutable` extension must be used. This extension will
+ :mod:`sqlalchemy.ext.mutable` extension must be used, most typically
+ using the :class:`.MutableDict` class. This extension will
allow "in-place" changes to the datastructure to produce events which
will be detected by the unit of work. See the example at :class:`.HSTORE`
for a simple example involving a dictionary.
+ Alternatively, assigning a JSON structure to an ORM element that
+ replaces the old one will always trigger a change event.
+
**Support for JSON null vs. SQL NULL**
When working with NULL values, the :class:`_types.JSON` type recommends the
@@ -2735,6 +2739,31 @@ class ARRAY(
:meth:`.types.ARRAY.Comparator.all`. The PostgreSQL-specific version of
:class:`_types.ARRAY` also provides additional operators.
+ .. container:: topic
+
+ **Detecting Changes in ARRAY columns when using the ORM**
+
+ The :class:`_sqltypes.ARRAY` type, when used with the SQLAlchemy ORM,
+ does not detect in-place mutations to the array. In order to detect
+ these, the :mod:`sqlalchemy.ext.mutable` extension must be used, using
+ the :class:`.MutableList` class::
+
+ from sqlalchemy import ARRAY
+ from sqlalchemy.ext.mutable import MutableList
+
+ class SomeOrmClass(Base):
+ # ...
+
+ data = Column(MutableList.as_mutable(ARRAY(Integer)))
+
+ This extension will allow "in-place" changes such to the array
+ such as ``.append()`` to produce events which will be detected by the
+ unit of work. Note that changes to elements **inside** the array,
+ including subarrays that are mutated in place, are **not** detected.
+
+ Alternatively, assigning a new array value to an ORM element that
+ replaces the old one will always trigger a change event.
+
.. versionadded:: 1.1.0
.. seealso::