Flask-SQLAlchemy使用SQLite数据库,即使它是为PostgreSQL配置的

时间:2016-05-17 13:16:58

标签: python flask sqlalchemy flask-sqlalchemy alembic

我在Heroku上有一个带有PostgreSQL数据库的Flask应用程序。我正在尝试运行我创建的迁移,以使用heroku run命令迁移我们的临时数据库。但是,我收到SQLite方言无法呈现JSON类型的错误。我没有使用SQLite。为什么我会收到此错误?

heroku run python manage.py db upgrade --app flask-practice-stage
Traceback (most recent call last):
  File "/app/.heroku/python/lib/python3.5/site-packages/sqlalchemy/sql/visitors.py", line 77, in _compiler_dispatch
    meth = getter(visitor)
AttributeError: 'SQLiteTypeCompiler' object has no attribute 'visit_JSON'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/app/.heroku/python/lib/python3.5/site-packages/sqlalchemy/sql/compiler.py", line 2159, in visit_create_table
    and not first_pk)
  File "/app/.heroku/python/lib/python3.5/site-packages/sqlalchemy/sql/compiler.py", line 213, in process
    return obj._compiler_dispatch(self, **kwargs)
  File "/app/.heroku/python/lib/python3.5/site-packages/sqlalchemy/sql/visitors.py", line 81, in _compiler_dispatch
    return meth(self, **kw)
  File "/app/.heroku/python/lib/python3.5/site-packages/sqlalchemy/sql/compiler.py", line 2190, in visit_create_column
    first_pk=first_pk
  File "/app/.heroku/python/lib/python3.5/site-packages/sqlalchemy/dialects/sqlite/base.py", line 853, in get_column_specification
    column.type, type_expression=column)
  File "/app/.heroku/python/lib/python3.5/site-packages/sqlalchemy/sql/compiler.py", line 261, in process
    return type_._compiler_dispatch(self, **kw)
  File "/app/.heroku/python/lib/python3.5/site-packages/sqlalchemy/sql/visitors.py", line 79, in _compiler_dispatch
    raise exc.UnsupportedCompilationError(visitor, cls)
sqlalchemy.exc.UnsupportedCompilationError: Compiler <sqlalchemy.dialects.sqlite.base.SQLiteTypeCompiler object at 0x7f90926e7f60> can't render element of type <class 'sqlalchemy.dialects.postgresql.json.JSON'>

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    manager.run()
  File "/app/.heroku/python/lib/python3.5/site-packages/flask_script/__init__.py", line 412, in run
    result = self.handle(sys.argv[0], sys.argv[1:])
  File "/app/.heroku/python/lib/python3.5/site-packages/flask_script/__init__.py", line 383, in handle
    res = handle(*args, **config)
  File "/app/.heroku/python/lib/python3.5/site-packages/flask_script/commands.py", line 216, in __call__
    return self.run(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.5/site-packages/flask_migrate/__init__.py", line 235, in upgrade
    command.upgrade(config, revision, sql=sql, tag=tag)
  File "/app/.heroku/python/lib/python3.5/site-packages/alembic/command.py", line 174, in upgrade
    script.run_env()
  File "/app/.heroku/python/lib/python3.5/site-packages/alembic/script/base.py", line 397, in run_env
    util.load_python_file(self.dir, 'env.py')
  File "/app/.heroku/python/lib/python3.5/site-packages/alembic/util/pyfiles.py", line 93, in load_python_file
    module = load_module_py(module_id, path)
  File "/app/.heroku/python/lib/python3.5/site-packages/alembic/util/compat.py", line 68, in load_module_py
    module_id, path).load_module(module_id)
  File "<frozen importlib._bootstrap_external>", line 385, in _check_name_wrapper
  File "<frozen importlib._bootstrap_external>", line 806, in load_module
  File "<frozen importlib._bootstrap_external>", line 665, in load_module
  File "<frozen importlib._bootstrap>", line 268, in _load_module_shim
  File "<frozen importlib._bootstrap>", line 693, in _load
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 662, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "migrations/env.py", line 87, in <module>
    run_migrations_online()
  File "migrations/env.py", line 80, in run_migrations_online
    context.run_migrations()
  File "<string>", line 8, in run_migrations
  File "/app/.heroku/python/lib/python3.5/site-packages/alembic/runtime/environment.py", line 797, in run_migrations
    self.get_context().run_migrations(**kw)
  File "/app/.heroku/python/lib/python3.5/site-packages/alembic/runtime/migration.py", line 312, in run_migrations
    step.migration_fn(**kw)
  File "/app/migrations/versions/260ae477b96f_.py", line 24, in upgrade
    sa.PrimaryKeyConstraint('id')
  File "<string>", line 8, in create_table
  File "<string>", line 3, in create_table
  File "/app/.heroku/python/lib/python3.5/site-packages/alembic/operations/ops.py", line 1098, in create_table
    return operations.invoke(op)
  File "/app/.heroku/python/lib/python3.5/site-packages/alembic/operations/base.py", line 318, in invoke
    return fn(self, operation)
  File "/app/.heroku/python/lib/python3.5/site-packages/alembic/operations/toimpl.py", line 101, in create_table
    operations.impl.create_table(table)
  File "/app/.heroku/python/lib/python3.5/site-packages/alembic/ddl/impl.py", line 194, in create_table
    self._exec(schema.CreateTable(table))
  File "/app/.heroku/python/lib/python3.5/site-packages/alembic/ddl/impl.py", line 118, in _exec
    return conn.execute(construct, *multiparams, **params)
  File "/app/.heroku/python/lib/python3.5/site-packages/sqlalchemy/engine/base.py", line 914, in execute
    return meth(self, multiparams, params)
  File "/app/.heroku/python/lib/python3.5/site-packages/sqlalchemy/sql/ddl.py", line 68, in _execute_on_connection
    return connection._execute_ddl(self, multiparams, params)
  File "/app/.heroku/python/lib/python3.5/site-packages/sqlalchemy/engine/base.py", line 962, in _execute_ddl
    compiled = ddl.compile(dialect=dialect)
  File "<string>", line 1, in <lambda>
  File "/app/.heroku/python/lib/python3.5/site-packages/sqlalchemy/sql/elements.py", line 494, in compile
    return self._compiler(dialect, bind=bind, **kw)
  File "/app/.heroku/python/lib/python3.5/site-packages/sqlalchemy/sql/ddl.py", line 26, in _compiler
    return dialect.ddl_compiler(dialect, self, **kw)
  File "/app/.heroku/python/lib/python3.5/site-packages/sqlalchemy/sql/compiler.py", line 190, in __init__
    self.string = self.process(self.statement, **compile_kwargs)
  File "/app/.heroku/python/lib/python3.5/site-packages/sqlalchemy/sql/compiler.py", line 213, in process
    return obj._compiler_dispatch(self, **kwargs)
  File "/app/.heroku/python/lib/python3.5/site-packages/sqlalchemy/sql/visitors.py", line 81, in _compiler_dispatch
    return meth(self, **kw)
  File "/app/.heroku/python/lib/python3.5/site-packages/sqlalchemy/sql/compiler.py", line 2170, in visit_create_table
    (table.description, column.name, ce.args[0])
  File "/app/.heroku/python/lib/python3.5/site-packages/sqlalchemy/util/compat.py", line 200, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "/app/.heroku/python/lib/python3.5/site-packages/sqlalchemy/util/compat.py", line 183, in reraise
    raise value.with_traceback(tb)
  File "/app/.heroku/python/lib/python3.5/site-packages/sqlalchemy/sql/compiler.py", line 2159, in visit_create_table
    and not first_pk)
  File "/app/.heroku/python/lib/python3.5/site-packages/sqlalchemy/sql/compiler.py", line 213, in process
    return obj._compiler_dispatch(self, **kwargs)
  File "/app/.heroku/python/lib/python3.5/site-packages/sqlalchemy/sql/visitors.py", line 81, in _compiler_dispatch
    return meth(self, **kw)
  File "/app/.heroku/python/lib/python3.5/site-packages/sqlalchemy/sql/compiler.py", line 2190, in visit_create_column
    first_pk=first_pk
  File "/app/.heroku/python/lib/python3.5/site-packages/sqlalchemy/dialects/sqlite/base.py", line 853, in get_column_specification
    column.type, type_expression=column)
  File "/app/.heroku/python/lib/python3.5/site-packages/sqlalchemy/sql/compiler.py", line 261, in process
    return type_._compiler_dispatch(self, **kw)
  File "/app/.heroku/python/lib/python3.5/site-packages/sqlalchemy/sql/visitors.py", line 79, in _compiler_dispatch
    raise exc.UnsupportedCompilationError(visitor, cls)
sqlalchemy.exc.CompileError: (in table 'results', column 'result_all'): Compiler <sqlalchemy.dialects.sqlite.base.SQLiteTypeCompiler object at 0x7f90926e7f60> can't render element of type <class 'sqlalchemy.dialects.postgresql.json.JSON'>
{'APPLICATION_ROOT': None,
 'CSRF_ENABLED': True,
 'DEBUG': True,
 'DEVELOPMENT': True,
 'JSONIFY_PRETTYPRINT_REGULAR': True,
 'JSON_AS_ASCII': True,
 'JSON_SORT_KEYS': True,
 'LOGGER_NAME': 'app',
 'MAX_CONTENT_LENGTH': None,
 'PERMANENT_SESSION_LIFETIME': datetime.timedelta(31),
 'PREFERRED_URL_SCHEME': 'http',
 'PRESERVE_CONTEXT_ON_EXCEPTION': None,
 'PROPAGATE_EXCEPTIONS': None,
 'SECRET_KEY': 'this-needs-to-be-changed',
 'SEND_FILE_MAX_AGE_DEFAULT': 43200,
 'SERVER_NAME': None,
 'SESSION_COOKIE_DOMAIN': None,
 'SESSION_COOKIE_HTTPONLY': True,
 'SESSION_COOKIE_NAME': 'session',
 'SESSION_COOKIE_PATH': None,
 'SESSION_COOKIE_SECURE': False,
 'SQLALCHEMY_DATABASE_URL': 'postgresql://localhost/flask_learning',
 'TESTING': False,
 'TRAP_BAD_REQUEST_ERRORS': False,
 'TRAP_HTTP_EXCEPTIONS': False,
 'USE_X_SENDFILE': False}

1 个答案:

答案 0 :(得分:2)

你有一个错字。配置密钥为SQLALCHEMY_DATABASE_URI,但您有URL。如果未配置Flask-SQLAlchemy,则默认为内存中的SQLite数据库。 SQLite不知道JSON列类型是什么,这是PostgreSQL特有的。