Problem with alembic revision --autogenerate: Empty migration file #1609
Replies: 1 comment 1 reply
-
✅ Solution Found: Empty `script.py.mako` File
I found the root cause of this issue. The problem was that my `script.py.mako` file (located in the `alembic_migrations` directory) was empty. This file is crucial as it serves as a template for Alembic to generate migration files.
### How to Fix:
1. Check if your `script.py.mako` is empty:
```bash
cat alembic_migrations/script.py.mako
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}
"""
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}
# revision identifiers, used by Alembic
revision = ${repr(up_revision)}
down_revision = ${repr(down_revision)}
branch_labels = ${repr(branch_labels)}
depends_on = ${repr(depends_on)}
def upgrade() -> None:
${upgrades if upgrades else "pass"}
def downgrade() -> None:
${downgrades if downgrades else "pass"}
rm -f alembic_migrations/versions/*
alembic stamp head
alembic revision --autogenerate -m "Initial migration" Why This Works:
As this was my first time using Alembic, I wasn't familiar with all its required files and their purposes. Hope this helps others who are also new to Alembic and encounter the same issue! 🚀 |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I am encountering an issue with Alembic when trying to generate an automatic migration.
🛠️ Context:
• This is my first time using Alembic.
• I am trying to generate a migration with the following command:
alembic revision --autogenerate -m "Initial migration"
FAILED: Could not determine revision id from filename
a3b8709870ad_initial_migration.py. Be sure the 'revision' variable is declared inside
the script (please see 'Upgrading from Alembic 0.1 to 0.2' in the documentation).
🧩 What I have checked so far:
• I verified that my SQLAlchemy models are properly registered:
python -c "from app.core.database import metadata; print(metadata.tables.keys())"
Output:
dict_keys(['users', 'trades', 'portfolios', 'assets', 'bots', 'dashboard_stats', 'bot_stats', 'account_stats', 'market_stats', 'user_settings', 'notification_settings', 'exchange_accounts', 'subscriptions', 'billing_history'])
✅ The tables are detected correctly by SQLAlchemy.
from app.core.database import engine, Base
target_metadata = Base.metadata
rm -rf alembic_migrations/versions/*.py
alembic revision --autogenerate -m "Initial migration"
alembic stamp head
alembic revision --autogenerate -m "Initial migration"
📌 My Questions:
• Why is Alembic not generating any migration content, even though SQLAlchemy correctly detects the models?
• How can I force Alembic to generate a valid file containing revision and down_revision?
• Could this be caused by a misconfiguration or a known bug in Alembic?
Any help would be greatly appreciated! 🙏🚀
Beta Was this translation helpful? Give feedback.
All reactions