JSON Type throws error when using Dialect that does not support it

Issue #4027 new
Adrian Partl created an issue

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)

  1. Adrian Partl 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.

  2. Log in to comment