Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to generate migrations for existing models. #156

Open
geekashu opened this issue Feb 1, 2025 · 8 comments
Open

Not able to generate migrations for existing models. #156

geekashu opened this issue Feb 1, 2025 · 8 comments
Labels
help wanted Extra attention is needed

Comments

@geekashu
Copy link
Contributor

geekashu commented Feb 1, 2025

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.

@igorbenav
Copy link
Owner

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

@igorbenav igorbenav added the help wanted Extra attention is needed label Feb 10, 2025
@danialt
Copy link

danialt commented Feb 11, 2025

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

@geekashu
Copy link
Contributor Author

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

@igorbenav
Copy link
Owner

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

@geekashu
Copy link
Contributor Author

Sure, will do soon.

@geekashu
Copy link
Contributor Author

geekashu commented Feb 18, 2025

Added the pull request. #167

@Jeremias333
Copy link

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

@Jeremias333
Copy link

from app.models import *

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

Works to me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants