Creation of flask-sqlalchemy tables

1

I'm developing a small application in Flask and I'm using the Flask-SQLAlchemy, Flask-Migrate, and Flask-Script extensions to manage my migrations in the database, following my application's configuration.

# -*- coding: utf-8 -*-

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate, MigrateCommand
from flask_script import Manager
from flask_login import LoginManager
from flask_mail import Mail
import coverage
import os
import unittest


# Aplicativo
app = Flask(__name__)

# Configurações
app.config.from_object(os.environ['APP_SETTINGS'])

# Database
db = SQLAlchemy(app)

# Migrações
migrate = Migrate(app, db)

# Manager
manager = Manager(app)
manager.add_command('db', MigrateCommand)

# Autenticação
login = LoginManager(app)
login.login_view = 'login'

# Mail
mail = Mail(app)

# Importações
from app import routes
from users import views, models

# Blueprints
app.register_blueprint(views.bp)

I start the database with flask db init , I create the migration with flask db migrate and update the database with flask db upgrade The problem is that when trying to use the database ecebo the following error:

sqlalchemy.exc.OperationalError

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: users [SQL: 'INSERT INTO users (username, password_hash, email) VALUES (?, ?, ?)'] [parameters: ('thiago', 'pbkdf2:sha256:50000$OINivJRi$d81223e55b8b4ef150c49c251c2007b756efa5cef51ed8bbdde66c263e20f2cf', 'tth*****@gmail.com')] (Background on this error at: http://sqlalche.me/e/e3q8)

When I then go into a flask shell session and do a db.create_all () the database does, but is it really necessary to do this manually? If yes, what file should I do this?

    
asked by anonymous 22.12.2018 / 04:04

1 answer

0

Post the file responsible for starting the project, usually when using the flask-script, the file responsible for starting the application, is something like this:

run.py file

from app import manager

if __name__=='__main__':
    manager.run()

to do the migration:  python run.py db init  python run.py db migrate

In other words, using flask-script is not necessary to use flask run.py db init etc, the own manager will do this.

    
22.12.2018 / 11:25