JSON Type throws error when using Dialect that does not support it
I want to use the JSON Column Type with the sqlite. This throws the following error, suggesting that this should work (i.e. JSON being serialised into string). However the code is broken at that point, since _json_serializer
is not part of the dialect. This should be a hasattr
test...
...
File "/Users/adrian/.virtualenvs/venv/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py", line 493, in <genexpr>
(key, value) for key, value in
File "/Users/adrian/.virtualenvs/venv/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py", line 496, in <genexpr>
for bindparam in self.bind_names)
File "/Users/adrian/.virtualenvs/venv/lib/python2.7/site-packages/sqlalchemy/sql/type_api.py", line 459, in _cached_bind_processor
d['bind'] = bp = d['impl'].bind_processor(dialect)
File "/Users/adrian/.virtualenvs/venv/lib/python2.7/site-packages/sqlalchemy/sql/sqltypes.py", line 2050, in bind_processor
json_serializer = dialect._json_serializer or json.dumps
StatementError: (exceptions.AttributeError) 'SQLiteDialect_pysqlite' object has no attribute '_json_serializer' [SQL: u'INSERT INTO metadata...
Comments (5)
-
repo owner -
reporter Not knowing your product decisions, I suspect that since you default back to serialising the JSON as a string, you save the string into a TEXT or BLOB. If this is indeed the case (which would work for backends not supporting JSON but TEXT/BLOB), I suspect the line should be
json_serializer = dialect._json_serializer if hasattr(dialect, "_json_serializer) and dialect._json_serializer else json.dumps
Otherwise raising NotImplementedError is also possible.
The documentation suggest, that NotImplementedError is the desired behaviour.
-
repo owner for plain JSON serialize to text with no SQL expression support and no native JSON type on the DB backend we have the Marshal JSON strings recipe.
For SQLite-specific JSON, SQLite now has JSON operators which would need to be implemented in some way for sqlalchemy.types.JSON, since sqlalchemy.types.JSON is documented as only for databases that have a native JSON type. The issue to resolve this is #3850 (where there is also a recipe).
-
reporter Then the bug is, that no sensible Exception is being raised.
-
repo owner - Log in to comment
what's the bug? that it doesn't throw a NotImplementedError() ?