14

I am reading (and watching) about Flask-Migrate here: https://realpython.com/blog/python/flask-by-example-part-2-postgres-sqlalchemy-and-alembic/ and here https://www.youtube.com/watch?v=YJibNSI-iaE#t=21

and doing everything from this tutorial:

  • I started a local postgres server (using Postgres.App, which started the server at postgresql://localhost:5432)
  • updated configs as per said tutorial
  • updated app.py, created a models.py etc.

After you install Flask-Migrate and run

python manage.py db init
python manage.py db migrate

it should detect all tables declared in models.py.

In my case, it detects nothing. And, based on the comments to the tutorial, it's not just my case. So, how do I make this work?

Mel
  • 5,837
  • 10
  • 37
  • 42
kurtgn
  • 8,140
  • 13
  • 55
  • 91

4 Answers4

27

Make sure your model is imported by your app. In most cases your views.py should do that. But you can also import it directly from your app.py.

Klaus D.
  • 13,874
  • 5
  • 41
  • 48
  • Hi, do you have an example for this? I am still encountering the issue. – Ryan Aquino Jul 21 '20 at 10:57
  • 1
    Hello sir @RyanAquino the file migrations/env.py have the line "from myapp import models" commented, just replace that with your data and that is it. – Luiz Jul 24 '20 at 00:56
4

Be sure that you're importing your models the same way throughout your application.

For example, I was using the following in my __init__.py:

from .models import *

And the following in my manage.py:

from databases import models

Because these have diff namespaces, flask_manager thinks there are duplicated tables. (Note, I found this solution in the following github issue / thread.)

Fix and all will be well.

Yaakov Bressler
  • 9,056
  • 2
  • 45
  • 69
2

If Flask can't detect your models, add this to env.py in migrations folder: from models import *

0

You need to import your model literally anywhere in your code, so SQLAlchemy can recognize it.

Cabaj
  • 11
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 06 '22 at 16:32