Flask-Migrate not detecting tables

2024/9/8 10:25:03

I have the following project structure:

project/__init__.py

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migratedb = SQLAlchemy()
migrate = Migrate()def create_app():app = Flask(__name__)app.config.from_object(os.environ['APP_SETTINGS'])db.init_app(app)migrate.init_app(app, db)return app

run.py

from project import create_app
app = create_app()if __name__ == "__main__":app.run()

manage.py

from flask_script import Manager
from flask_migrate import MigrateCommand
from project.models import *
from project import create_appmanager = Manager(create_app)
manager.add_command('db', MigrateCommand)if __name__ == '__main__':manager.run()

Yet when I run the following commands, Flask-Migrate is not detecting any tables to be added.

python manage.py db init

Which outputs:

Creating directory $HOME/Project/migrations ... done
Creating directory $HOME/Project/migrations/versions ... done
Generating $HOME/Project/migrations/script.py.mako ... done
Generating $HOME/Project/migrations/env.py ... done
Generating $HOME/Project/migrations/README ... done
Generating $HOME/Project/migrations/alembic.ini ... done
Please edit configuration/connection/logging settings in
'$HOME/Project/migrations/alembic.ini' before proceeding.

and

python manage.py db migrate

Which only outputs:

INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO  [alembic.runtime.migration] Will assume transactional DDL.

Why is Flask-Migrate with Alembic not detecting the Models and therefore creating the tables? Here's what I've tried:

  • Deleting the database, starting from nothing
  • Creating a custom db class inside the manage.py file, doesn't detect that
  • Googling every answer to this problem, found lots of similar questions but none of their solutions worked for me.

EDIT:

Here is an example of the models.py file

from flask import current_app
from project import db
from flask_login import UserMixinclass User(db.Model, UserMixin):id = db.Column(db.Integer, primary_key=True)name = db.Column(db.String(50))
Answer

The solution was to import the models in the __init__.py file like so:

def create_app():app = Flask(__name__)app.config.from_object(os.environ['APP_SETTINGS'])from project import modelsdb.init_app(app)migrate.init_app(app, db)return app
https://en.xdnf.cn/q/73126.html

Related Q&A

Cannot train a neural network solving XOR mapping

I am trying to implement a simple classifier for the XOR problem in Keras. Here is the code:from keras.models import Sequential from keras.layers.core import Dense, Dropout, Activation from keras.optim…

Pyarrow read/write from s3

Is it possible to read and write parquet files from one folder to another folder in s3 without converting into pandas using pyarrow.Here is my code:import pyarrow.parquet as pq import pyarrow as pa imp…

matplotlib draw showing nothing

Im using pythons matplotlib to do some contours using contour and contourf functions. They all work fine when using show, but when I try to use draw() inside a method, I get the matplotlib window but n…

Measure the height of a string in Tkinter Python?

I need the height in pixel of a string in a Tkiner widget. It is the text in a row of a Listbox.I know I can measure the width of a string with tkinter.font.Font.measure. But how can I get the height?…

pandas filter by multiple columns NULL

I have a pandas dataframe like:df = pd.DataFrame({Last_Name: [Smith, None, Brown], First_Name: [John, None, Bill],Age: [35, 45, None]})And could manually filter it using:df[df.Last_Name.isnull() & …

Closed IPython Notebook that was running code

How it works? I got some code running in an IPython Notebook. Some iterative work. Accidentally I closed the browser with the running Notebook, but going back to the IPython Dashboard I see that this …

os.popen().read() - charmap decoding error

I have already read UnicodeDecodeError: charmap codec cant decode byte X in position Y: character maps to <undefined>. While the error message is similar, the code is completely different, becau…

multiprocessing numpy not defined error

I am using the following test code:from pathos.multiprocessing import ProcessingPool as Pool import numpydef foo(obj1, obj2):a = obj1**2b = numpy.asarray(range(1,5))return obj1, bif __name__ == __main_…

Convert mp4 sound to text in python

I want to convert a sound recording from Facebook Messenger to text. Here is an example of an .mp4 file send using Facebooks API: https://cdn.fbsbx.com/v/t59.3654-21/15720510_10211855778255994_5430581…

pandas map one column to the combination of two columns

I am working with a DataFrame which looks like thisList Numb Name 1 1 one 1 2 two 2 3 three 4 4 four 3 5 fiveand I am trying to compute…