Skip to content

How to configure alembic autogenerate with multiple model files #1157

Description

@lehoangnam040

Describe the bug
Have a versions file that create Account. After add another Product model, Run alembic revision --autogenerate -m "..." doesn't create new table Product but drop current table Account on new ..py in versions

To Reproduce

  • Project structure
.
├── devops
│   ├── db_migration
│   │   ├── alembic.ini
│   │   └── pg
│   │       ├── env.py
│   │       └── versions
│   │           ├── d65c5dec925e_create_account_table.py
├── service
│   ├── __init__.py
│   ├── databases
│   │   ├── __init__.py
│   │   ├── postgres
│   │   │   ├── __init__.py
│   │   │   ├── account.py
│   │   │   ├── product.py
  • devops/db_migration/pg/env.py
myPath = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, myPath + "/../../../")

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

from service.databases.postgres import DB_URL, metadata
target_metadata = metadata
...
  • devops/db_migration/pg/versions/d65c5dec925e_create_account_table.py
revision = 'd65c5dec925e'
down_revision = None
branch_labels = None
depends_on = None
def upgrade() -> None:
    op.create_table('account',
    sa.Column('uid', sa.BigInteger(), autoincrement=False, nullable=False),
    ...
    )
def downgrade() -> None:
    op.drop_table('account')
  • service/databases/__init__.py
DB_URL = f"postgresql://..."
metadata = sqlalchemy.MetaData()
database = databases.Database(DB_URL)

class PgBaseMeta(ormar.ModelMeta):
    database = database
    metadata = metadata
  • service/databases/account.py
from . import PgBaseMeta
class PgAccount(ormar.Model):
    class Meta(PgBaseMeta):
        tablename = "account"
    uid = ormar.BigInteger(autoincrement=False, primary_key=True)
    username = ormar.String(max_length=64, unique=True)
  • service/databases/product.py
from . import PgBaseMeta
class PgProduct(ormar.Model):
    class Meta(PgBaseMeta):
        tablename = "product"
    pid = ormar.BigInteger(autoincrement=False, primary_key=True)
    product_name = ormar.String(max_length=256)
  • Run command ...devops/db_migration $ alembic revision --autogenerate -m "create product table" create new file devops/db_migration/pg/versions/64ec73d106d7_create_product_table.py
revision = '64ec73d106d7'
down_revision = 'd65c5dec925e'
branch_labels = None
depends_on = None
def upgrade() -> None:
    op.drop_table('account')
def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('account',
    ...
    )

Expected behavior

  • File devops/db_migration/pg/versions/64ec73d106d7_create_product_table.py should be
revision = '64ec73d106d7'
down_revision = 'd65c5dec925e'
branch_labels = None
depends_on = None
def upgrade() -> None:
    op.create_table('product')
def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('product',
    ...
    )

Versions (please complete the following information):

  • postgresql 14
  • Python version 3.11.4
  • ormar version 0.12.2
  • pydantic version 1.10.8
  • fastapi version 0.99.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions