[Python] Prevent Reflex (Using Alembic) from generating migrations of existing tables in the...

Discussão em 'Python' iniciado por Stack, Agosto 29, 2025.

  1. Stack

    Stack Membro Participativo

    I am using Reflex with Alembic to manage migrations.

    My problem is that when I run reflex db makemigrations, Alembic includes all the tables that already exist in the database, even if they are not defined as models in my application.

    I only want Alembic to generate migrations for my models declared with rx.Model, and to ignore any existing tables.

    I have seen that Alembic can be configured from env.py, but when I test with Reflex, it does not seem to be executing any code here. It does execute code when I run the Alembic commands.

    alembic/env.py

    from logging.config import fileConfig
    from sqlalchemy import engine_from_config
    from sqlalchemy import pool
    from alembic import context
    from reflex.model import Model
    from agendaReflex.models.user.model import User

    config = context.config

    if config.config_file_name is not None:
    fileConfig(config.config_file_name)

    target_metadata = Model.metadata

    ALLOWED_TABLES = Model.metadata.tables.keys()
    print("ALLOWED TABLES:", ALLOWED_TABLES)
    def include_object(obj, name, type\_, reflected, compare_to):
    return name in ALLOWED_TABLES



    IF I RUN:

    alembic revision --autogenerate -m "init"

    I GET:

    ALLOWED_TABLES: dict_keys(\['USERS_NUE'\])
    INFO \[alembic.runtime.migration\] Context impl PostgresqlImpl.
    INFO \[alembic.runtime.migration\] Will assume transactional DDL.
    Included table: USERS_NUE
    INFO \[alembic.autogenerate.compare\] Detected added table 'USERS_NUE'
    INFO \[alembic.ddl.postgresql\] Detected sequence named 'empleados_id_seq' as owned by integer column 'empleados(id)', assuming SERIAL and omitting
    Excluded table: empleados


    IF I RUN:

    reflex db makemigrations -m "init"

    I GET:

    A migration without my models and with all my database tables included.

    How can I make reflex db makemigrations respect my env.py and Alembic only generate migrations for my models (rx.Model), without attempting to recreate tables that already exist in the database?

    Continue reading...

Compartilhe esta Página