sqlite 参数“render_as_batch”无效

dfty9e19  于 2023-03-03  发布在  SQLite
关注(0)|答案(1)|浏览(116)

我尝试让Alembic自动迁移SQLite中的表字段类型更改。根据documentation,要做到这一点,请将render_as_batch=True添加到上下文。通常,我们需要添加compare_type=True,但SQLite不支持直接类型更改。

context.configure(
    connection=connection,
    target_metadata=target_metadata,
    render_as_batch=True <----addition here
)

. env文件:

def run_migrations_offline() -> None:
    url = config.get_main_option("sqlalchemy.url")
    context.configure(
        url=url,
        target_metadata=target_metadata,
        literal_binds=True,
        dialect_opts={"paramstyle": "named"},
        render_as_batch=True, <----here
    )
***
def run_migrations_online() -> None:
    """Run migrations in 'online' mode.

    In this scenario we need to create an Engine
    and associate a connection with the context.

    """
    connectable = engine_from_config(
        config.get_section(config.config_ini_section),
        prefix="sqlalchemy.",
        poolclass=pool.NullPool,
        render_as_batch=True, <---here
    )

在alembic.ini中,我将sqlalchemy. url设置为sqlalchemy.url = sqlite:///app/openpapers.db,它会检测到迁移,并自动生成迁移。然而,当我使用render_as_batch安装程序运行迁移时,我得到了这个错误:

TypeError: Invalid argument(s) 'render_as_batch' sent to create_engine(), using configuration SQLiteDialect_pysqlite/NullPool/Engine.  Please check that the keyword arguments are appropriate for this combination of components

我使用的是SQLModel,因此www.example.com包含了其他行:env.py contains additional lines:

# from sqlalchemy import engine_from_config <----I've tried commenting this out
from sqlalchemy import pool
from sqlmodel import SQLModel, engine_from_config <----Added this, still same error.

from app.models import various_sqlmodel_models <----SQLModels imported here

target_metadata = SQLModel.metadata

如何让这个render_as_batch模式与SQLModel、SQLAlchemy和SQLite一起工作?我以为SQLAlchemy SQLite方言支持这一点,但我得到了错误:

File "/home/phillyharper/openpapers/.venv/bin/alembic", line 8, in <module>
    sys.exit(main())
  File "/home/phillyharper/openpapers/.venv/lib/python3.10/site-packages/alembic/config.py", line 590, in main
    CommandLine(prog=prog).main(argv=argv)
  File "/home/phillyharper/openpapers/.venv/lib/python3.10/site-packages/alembic/config.py", line 584, in main
    self.run_cmd(cfg, options)
  File "/home/phillyharper/openpapers/.venv/lib/python3.10/site-packages/alembic/config.py", line 561, in run_cmd
    fn(
  File "/home/phillyharper/openpapers/.venv/lib/python3.10/site-packages/alembic/command.py", line 229, in revision
    script_directory.run_env()
  File "/home/phillyharper/openpapers/.venv/lib/python3.10/site-packages/alembic/script/base.py", line 569, in run_env
    util.load_python_file(self.dir, "env.py")
  File "/home/phillyharper/openpapers/.venv/lib/python3.10/site-packages/alembic/util/pyfiles.py", line 94, in load_python_file
    module = load_module_py(module_id, path)
  File "/home/phillyharper/openpapers/.venv/lib/python3.10/site-packages/alembic/util/pyfiles.py", line 110, in load_module_py
    spec.loader.exec_module(module)  # type: ignore
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/home/phillyharper/openpapers/migrations/env.py", line 88, in <module>
    run_migrations_online()
  File "/home/phillyharper/openpapers/migrations/env.py", line 71, in run_migrations_online
    connectable = engine_from_config(
  File "/home/phillyharper/openpapers/.venv/lib/python3.10/site-packages/sqlalchemy/engine/create.py", line 743, in engine_from_config
    return create_engine(url, **options)
  File "<string>", line 2, in create_engine
  File "/home/phillyharper/openpapers/.venv/lib/python3.10/site-packages/sqlalchemy/util/deprecations.py", line 309, in warned
    return fn(*args, **kwargs)
  File "/home/phillyharper/openpapers/.venv/lib/python3.10/site-packages/sqlalchemy/engine/create.py", line 636, in create_engine
    raise TypeError(
TypeError: Invalid argument(s) 'render_as_batch' sent to create_engine(), using configuration SQLiteDialect_pysqlite/NullPool/Engine.  Please check that the keyword arguments are appropriate for this combination of components.

如果我把方言改成PostgreSQL,问题仍然存在。任何对context.configure()的添加都会产生这个问题。

mftmpeh8

mftmpeh81#

您犯了和我一样的错误。您正在将render_as_batch=True添加到engine_from_config()而不是context.configure()。有关联机迁移,请参阅contex.configure()的do_run_migrations函数

相关问题