From 01cbf4d7b8acab54a054bb36dc2792b518b5cd1f Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Thu, 7 Nov 2019 15:31:48 -0500 Subject: 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 --- lib/sqlalchemy/dialects/postgresql/json.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'lib/sqlalchemy/dialects/postgresql/json.py') 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 )``):: 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, -- cgit v1.2.1