diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-11-07 15:31:48 -0500 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-11-11 14:37:55 -0500 |
commit | 01cbf4d7b8acab54a054bb36dc2792b518b5cd1f (patch) | |
tree | 09e59ba5a962f02444ca39d8f242c0b3bd86cdb0 /lib/sqlalchemy/dialects/postgresql/json.py | |
parent | bbe754784ae4630dd0ebf30d3bc2be566f8a8fef (diff) | |
download | sqlalchemy-01cbf4d7b8acab54a054bb36dc2792b518b5cd1f.tar.gz |
Add type accessors for JSON indexed/pathed element access
Added new accessors to expressions of type :class:`.JSON` to allow for
specific datatype access and comparison, covering strings, integers,
numeric, boolean elements. This revises the documented approach of
CASTing to string when comparing values, instead adding specific
functionality into the PostgreSQL, SQlite, MySQL dialects to reliably
deliver these basic types in all cases.
The change also delivers a new feature to the test exclusions
system so that combinations and exclusions can be used together.
Fixes: #4276
Change-Id: Ica5a926c060feb40a0a7cd60b9d6e061d7825728
Diffstat (limited to 'lib/sqlalchemy/dialects/postgresql/json.py')
-rw-r--r-- | lib/sqlalchemy/dialects/postgresql/json.py | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/lib/sqlalchemy/dialects/postgresql/json.py b/lib/sqlalchemy/dialects/postgresql/json.py index b8e3ac23d..ef48f3d86 100644 --- a/lib/sqlalchemy/dialects/postgresql/json.py +++ b/lib/sqlalchemy/dialects/postgresql/json.py @@ -123,11 +123,17 @@ class JSON(sqltypes.JSON): data_table.c.data['some key'].astext == 'some value' + Note that equivalent functionality is available via the + :attr:`.JSON.Comparator.as_string` accessor. + * Index operations with CAST (equivalent to ``CAST(col ->> ['some key'] AS <type>)``):: data_table.c.data['some key'].astext.cast(Integer) == 5 + Note that equivalent functionality is available via the + :attr:`.JSON.Comparator.as_integer` and similar accessors. + * Path index operations (the ``#>`` operator):: data_table.c.data[('key_1', 'key_2', 5, ..., 'key_n')] @@ -214,7 +220,6 @@ class JSON(sqltypes.JSON): :meth:`.ColumnElement.cast` """ - if isinstance(self.expr.right.type, sqltypes.JSON.JSONPathType): return self.expr.left.operate( JSONPATH_ASTEXT, |