Alembic 自动生成每次迁移都会重新创建表

时间:2021-07-08 08:31:30

标签: python postgresql sqlalchemy alembic

我使用 Alembic、sqlalchemy、Postgresql。 创建迁移 ' alembic revision --autogenerate -m "First"' 时,alembic 每次都会重新创建表。如何重新配置​​它以使其仅进行更改?

env.py

from logging.config import fileConfig

from sqlalchemy import engine_from_config
from sqlalchemy import pool

from alembic import context

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config

# Interpret the config file for Python logging.
# This line sets up loggers basically.
fileConfig(config.config_file_name)

from app.models.map import Base

# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
target_metadata = Base.metadata

# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.


def run_migrations_offline():
    """Run migrations in 'offline' mode.

    This configures the context with just a URL
    and not an Engine, though an Engine is acceptable
    here as well.  By skipping the Engine creation
    we don't even need a DBAPI to be available.

    Calls to context.execute() here emit the given string to the
    script output.

    """
    url = config.get_main_option("sqlalchemy.url")
    context.configure(
        url=url,
        target_metadata=target_metadata,
        literal_binds=True,
        dialect_opts={"paramstyle": "named"},
        include_schemas=True
    )

    with context.begin_transaction():
        context.run_migrations()


def run_migrations_online():
    """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,
    )

    with connectable.connect() as connection:
        context.configure(
            connection=connection,
            target_metadata=target_metadata
        )

        with context.begin_transaction():
            context.run_migrations()


if context.is_offline_mode():
    run_migrations_offline()
else:
    run_migrations_online()

在配置中添加 `include_schemas=True ' 没有帮助。

这就是班级的样子。

class Person(Base):
    """Person in department class."""

    __tablename__ = "person"
    __table_args__ = {"schema": "core"}

    id = Column(TEXT, primary_key=True, nullable=False)
    status = Column(TEXT, nullable=False)
    created_utc = Column(TIMESTAMP(timezone=False), nullable=False)
    updated_utc = Column(TIMESTAMP(timezone=False), nullable=False)
    subject_id = Column(TEXT, nullable=True)
    lastname = Column(TEXT, nullable=True)
    firstname = Column(TEXT, nullable=True)
    middlename = Column(TEXT, nullable=True)
    birthday = Column(DATE, nullable=True)
    awards = relationship("Awards", back_populates="in_person")

    def __repr__(self):
        return "{} {} {}".format(self.lastname, self.firstname, self.middlename)

在不同的方案中有几个这样的类。

0 个答案:

没有答案
相关问题