3

I'm trying to have Alembic auto-migrate table field type changes in SQLite. According to documentation to do this add render_as_batch=True to the context. Ordinarily, we would need to add compare_type=True but SQLite doesn't support direct type changes.

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

.env file:

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
    )

In alembic.ini I have sqlalchemy.url set to sqlalchemy.url = sqlite:///app/openpapers.db which it detects, migrations are found and made on autogenerate. However, when I run them with render_as_batch setup I get this error:

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

I'm using SQLModel so 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

How to get this render_as_batch pattern to work with SQLModel, SQLAlchemy, and SQLite? I thought the SQLAlchemy SQLite dialect supported this, but I get the error:

 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.

If I change dialect to PostgreSQL problem persists. Any addition to context.configure() creates this problem.

user4157124
  • 2,809
  • 13
  • 27
  • 42
phil0s0pher
  • 525
  • 10
  • 21

1 Answers1

3

you made the same mistake as me. You are adding render_as_batch=True to engine_from_config() instead of context.configure(). See the function do_run_migrations for the contex.configure() for online migrations

Jorrit Smit
  • 81
  • 1
  • 6