3

According to this answer SQLite supports JSON data since version 3.9. I use version 3.24 in combination with SQLALchemy (1.2.8) and Python 3.6, but I cannot create any tables containing JSON columns.

What am I missing or doing wrong? A minimal (not) working example is given below:

import sqlalchemy as sa
import os
import tempfile

metadata = sa.MetaData()

foo = sa.Table(
    'foo',
    metadata,
    sa.Column('bar', sa.JSON)
)

tmp_dir = tempfile.mkdtemp()
dbname = os.path.join(tmp_dir, 'foo.db')

engine = sa.create_engine('sqlite:////' + dbname)

metadata.bind = engine

metadata.create_all()

This fails giving the following error:

sqlalchemy.exc.CompileError: (in table 'foo', column 'bar'): Compiler <sqlalchemy.dialects.sqlite.base.SQLiteTypeCompiler object at 0x7f1eae1dab70> can't render element of type <class 'sqlalchemy.sql.sqltypes.JSON'>

Thanks!

1
  • 2
    Basic JSON1 support should be coming in 1.3 for SQLite dialect. Your example will work as is with it. Commented Jul 16, 2018 at 18:03

1 Answer 1

5

Use a TEXT column. Sqlite has a JSON extension with some functions for working with JSON data, but no dedicated JSON type.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.