'flask-sqlalchemy's db.create_all() doesn't create tables

I want to create new tables in my postgresdb but db.create_all() doesn't seem to be doing anything.

I have a models class where I define all the tables, then in the __init__.py, I import the models.py file.

I get no errors, and debugging shows that it enters the file indeed and reach the db.create_all() line but does nothing with it.

models.py

from flask import current_app as app
from flask_sqlalchemy import SQLAlchemy

app.config['SQLALCHEMY_DATABASE_URI'] = f"postgresql://postgres:{os.getenv('POSTGRES_USER_PASSWORD')}@localhost/postgres"
db = SQLAlchemy(app)

class Visitor(db.Model):
    __tablename__ = 'visitors'

    id = db.Column(db.Integer, primary_key=True)
    fullname = db.Column(db.String(256))
    key = db.Column(db.String(256), unique=True)
    expire_date = db.Column(db.DateTime)
    location = db.Column(db.String)   
    company = db.Column(db.String)
    reason_of_visit = db.Column(db.String)
    created_at = db.Column(db.DateTime, default=datetime.utcnow().replace(microsecond=0))

    VISITOR_CREATE  = 'Issue Visitor Card'
    VISITOR_RETURN = 'Return Visitor Card'



db.create_all()

__init__.py

from flask import Flask

app = Flask(__name__)
with app.app_context():
    from .api import models

The tables were created normally when I used SQLAlchemy instead of Flask-SQLAlchemy.

I know there are multiple questions for this topic but I couldn't find anything similar to my issue.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source