1

I'm attempting to run flask migrate db in my working directory, and it does not use the model I defined in models.py Here's the code.

models.py

import sys
sys.path.append("../")

from Talks2 import db

class Talk(db.Model):
    presenter = db.Column(db.Text())
    talkType = db.Column(db.Text())
    desc = db.Column(db.Text(), primary_key=True)
    link = db.Column(db.Text())
    time = db.Column(db.Integer())

    def __repr__(self):
        return "Presenter: {}\nType: {}\nDescription:\n{}\nLink:  {}".format(self.presenter,self.talkType,self.desc,self.link)

routes.py

import sys
sys.path.append("../")

from flask import Flask, request, render_template
from Talks2 import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate

app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app,db)

from Talks2 import models

@app.route("/")
def index():
    return render_template("index.html")

@app.route("/add", methods=["POST"])
def add():
    person = request.form["presenter"]
    ttype = request.form["type"]
    desc = request.form["desc"]
    link = request.form["link"]
    print(person, file=sys.stderr)
    print(ttype, file=sys.stderr)
    print(desc, file=sys.stderr)
    print(link, file=sys.stderr)
    return render_template("index.html")

if __name__ == "__main__":
    app.run()

What do I need to change for it to correctly generate the script?

Josh Gordon
  • 127
  • 2
  • 9
  • The command is `flask db migrate` not `flask migrate db`. If that is not the issue, it would help to see the output of the actual command as well as the generated version file. – Matthew Story Jun 30 '18 at 04:39

1 Answers1

-1

You are importing db from Talks2.py in models.py file and again in routes.py declaring again.

You haven't shared the code of Talks2.py file. What I am suspecting is you are declaring app and db object multiple times and replacing it with others.

Just do import in the proper way and your model will be detected by the flask.

The simplest solution is to declare app & db in Talks2.py, then import both in models.py and then from models.py import app & db in routes.py. This will resolve your problem.

Also, it should be flask db migrate instead of flask migrate db.

For more information refer to these commands:

To create a migration repository:

flask db init

To Generate Migration Script (Make sure to reviewed and edited, as Alembic currently does not detect every change you make to your models)

flask db migrate

To apply the migration to the database

flask db upgrade

To see all the commands that are available run this command:

flask db --help

For more info refer this official doc.

Let me know if this didn't help.

Harshad Kavathiya
  • 8,939
  • 1
  • 10
  • 19