3

I know there is a typical question, but what am I doing wrong?
After set

FLASK_APP='safecanyons:app'
flask run

the application starts but no one table is created. This is the main script:

from flask import Flask, request, jsonify, g
from flask_sqlalchemy import SQLAlchemy
from flask_jwt_extended import JWTManager, jwt_required
from safecanyons.models.users import User
from safecanyons.v1.users import bp as bp_users
from safecanyons.settings.dev import SQLALCHEMY_DATABASE_URI
from psycopg2 import connect
from flask_sqlalchemy import SQLAlchemy
from safecanyons.models.users import User

def create_app():
    # The imports :
    # from safecanyons.settings.dev import SQLALCHEMY_DATABASE_URI
    
    # SQLALCHEMY_DATABASE_URI = "postgresql://{}:{}@{}/{}".format(
    # DB_USER, DB_PASSWORD, DB_HOST,DB_NAME
    # ) 
    # 
    # from safecanyons.models.users import User
    # 
    # from flask_sqlalchemy import SQLAlchemy
    # db = SQLAlchemy()

    # class User(db.Model):
    # __tablename__ = 'users'
    
    # id = db.Column(db.Integer, primary_key=True)
    # username = db.Column(db.String(80), unique=True, nullable=False)
    # email = db.Column(db.String(120), unique=True, nullable=False)

    # def __repr__(self):
    #     return '<User %r>' % self.username
      
    uri = SQLALCHEMY_DATABASE_URI
    app = Flask('safecanyons')
    app.config['SQLALCHEMY_DATABASE_URI'] = uri
    SQLALCHEMY_TRACK_MODIFICATIONS = True
    db = SQLAlchemy(app)
    from safecanyons.models.users import User

    db.create_all()
  
    jwt = JWTManager(app)
    app.register_blueprint(bp_users,url_prefix='/v1/users')
    return app,db

app,db = create_app()


@app.route("/")
def hello_world():
    return "<p>Hello, World!</p>"

I've tried with the app.app_context() but it doesn't work as I expect. When I go to database to check the users table creation, no one table exists.

4
  • But when you execute db.create_all()? You must do it explicitly. Commented Dec 10, 2021 at 7:28
  • Inside create_app() method, db.create_db() is called Commented Dec 10, 2021 at 15:02
  • When I run flask I do: export FLASK_APP=‘safecanyons:app’ so I think create_app() is called then. Also after your comment I’ve tried to call manually after flask run was raised an also still not working(not creating the models) Commented Dec 10, 2021 at 17:54
  • Are you sure that User model definition is imported before create_all(). Becuaeu you have import but also commented code. You can try to put User Commented Dec 10, 2021 at 18:09

1 Answer 1

2

You probably have problems with importing User model in the right way (because when using create_app pattern there is circular import challange, and you are doing strange hack with returning db from create_app). User must be defined before create_all call, but User to be defined need db but also create_all need. How to solve it (I may be wrong with file names and project structure):

  1. safecanyons/app.py
from flask_jwt_extended import JWTManager, jwt_required
from flask_sqlalchemy import SQLAlchemy
from psycopg2 import connect

from safecanyons.v1.users import bp as bp_users
from safecanyons.settings.dev import SQLALCHEMY_DATABASE_URI


db = SQLAlchemy()

def create_app():
    from safecanyons.models.users import User
 
    uri = SQLALCHEMY_DATABASE_URI
    app = Flask('safecanyons')
    app.config['SQLALCHEMY_DATABASE_URI'] = uri
    SQLALCHEMY_TRACK_MODIFICATIONS = True

    db.init_app(app) # init of db is deferred
    db.create_all()

    jwt = JWTManager(app)
    app.register_blueprint(bp_users,url_prefix='/v1/users')
    return app
   
app = create_app()

@app.route("/")
def hello_world():
    return "<p>Hello, World!</p>"
  1. safecanyons/models/users.py:
from safecayons.app import db

class User(db.Model):
    __tablename__ = 'users'
    
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)

    def __repr__(self):
        return '<User %r>' % self.username

The other solution is place above code in one file, but model User should be defined before create_all call.

Similar topics:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.