Not able to generate migrations for existing models.
Hi Team,
I have disabled create_tables_on_start like app = create_application(router=router, settings=settings, create_tables_on_start=False).
Now on my local whenever I try to run poetry run alembic revision --autogenerate, it always create an empty migration file. I don't want to use create_all() to create the tables in my DB, instead I want to go via the alembic migrations way and generate all the required migrations beforehand. Is there a step am I missing?
I am coming from the Django world and trying to learning FastAPI using this boilerplate.
Hey, @geekashu, weird. I can't replicate this, weirdly it works fine for me. Anyone else with this issue?
You can use these changes to get to migration on Mac M4 using from scratch method:
File: pyproject.toml
diff --git a/pyproject.toml b/pyproject.toml
index 2e6abc8..bda3aa8 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -11,8 +11,8 @@ packages = [{ include = "src" }]
python = "^3.11"
python-dotenv = "^1.0.0"
pydantic = { extras = ["email"], version = "^2.6.1" }
-fastapi = "^0.109.1"
-uvicorn = "^0.27.0"
+fastapi = "^0.109.2"
+uvicorn = "^0.27.1"
uvloop = "^0.19.0"
httptools = "^0.6.1"
uuid = "^1.30"
@@ -23,7 +23,7 @@ python-jose = "^3.3.0"
SQLAlchemy = "^2.0.25"
pytest = "^7.4.2"
python-multipart = "^0.0.9"
-greenlet = "^2.0.2"
+greenlet = "^3.0.3"
httpx = "^0.26.0"
pydantic-settings = "^2.0.3"
redis = "^5.0.1"
File: src/alembic.ini
diff --git a/src/alembic.ini b/src/alembic.ini
index 07489da..3cd71d6 100644
--- a/src/alembic.ini
+++ b/src/alembic.ini
@@ -2,7 +2,7 @@
[alembic]
# path to migration scripts
-script_location = migrations
+script_location = src/migrations
# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
# Uncomment the line below if you want the files to be prepended with date and time
File: src/migrations/env.py
diff --git a/src/migrations/env.py b/src/migrations/env.py
index dbfd5a8..4422e85 100644
--- a/src/migrations/env.py
+++ b/src/migrations/env.py
@@ -1,7 +1,13 @@
import asyncio
+import os
+import sys
from logging.config import fileConfig
from alembic import context
+
+# Add the src directory to the Python path
+sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
+
from app.core.config import settings
from app.core.db.database import Base
from sqlalchemy import pool
Hey,
Thanks for the reply. Yeah, I am using M4 Pro. The above didn't work for me.
What worked was the addition of the following block of code in env.py file -
# Auto-import all models in app.models
def import_models(package_name):
package = importlib.import_module(package_name)
for _, module_name, _ in pkgutil.walk_packages(package.__path__, package.__name__ + "."):
importlib.import_module(module_name)
# Load all models dynamically
import_models("app.models")
That is good, @geekashu, do you want to open a pr with it?
Sure, will do soon.
Added the pull request. #167
I'm on Linux, both cases didn't worked to me
from app.models import *
This one in env.py + poetry run alembic revision --autogenerate
Works to me
Closed by #167