Creation of flask-sqlalchemy tables

Asked

Viewed 692 times

0

I am developing a small application in Flask and I am using the extensions Flask-Sqlalchemy, Flask-Migrate and Flask-Script to manage my migrations in the database, follow the configuration of my application.

# -*- 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, create migration with flask db migrate and update the database with flask db upgrade the problem is that when trying to use the database I get 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 log into a flask shell session and do a db.create_all() database function, but is it really necessary to do it manually? If yes in which file should I do this?

  • The problem is that the table was not created before you tried to enter the data. You checked whether the first migration generated by flask-migrate are creating these tables? How are some migrations?

1 answer

0

post the file responsible for starting the project, usually using flask-script, the file responsible for starting the application, is more or less like this :

run py file.

from app import manager

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

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

ie au using flask-script it is not necessary to use flask run.py db init etc, the own manager will do this.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.