FastAPI-boilerplate icon indicating copy to clipboard operation
FastAPI-boilerplate copied to clipboard

Not able to generate migrations for existing models.

Open geekashu opened this issue 10 months ago • 8 comments

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.

geekashu avatar Feb 01 '25 02:02 geekashu

Hey, @geekashu, weird. I can't replicate this, weirdly it works fine for me. Anyone else with this issue?

igorbenav avatar Feb 10 '25 22:02 igorbenav

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

danialt avatar Feb 11 '25 15:02 danialt

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")

geekashu avatar Feb 17 '25 09:02 geekashu

That is good, @geekashu, do you want to open a pr with it?

igorbenav avatar Feb 18 '25 02:02 igorbenav

Sure, will do soon.

geekashu avatar Feb 18 '25 02:02 geekashu

Added the pull request. #167

geekashu avatar Feb 18 '25 08:02 geekashu

I'm on Linux, both cases didn't worked to me

Jeremias333 avatar Feb 20 '25 20:02 Jeremias333

from app.models import *

This one in env.py + poetry run alembic revision --autogenerate

Works to me

Jeremias333 avatar Feb 20 '25 21:02 Jeremias333

Closed by #167

igorbenav avatar Jun 11 '25 23:06 igorbenav