Alex Dana
Alex Dana

Reputation: 1307

Assertion error with flask and SQL Alchemy

I have the following flask structuration :

enter image description here

With the following code :

init.py

from press_app.views import app
from press_app import models

# Connect sqlalchemy to app
models.db.init_app(app)

@app.cli.command()
def init_db():
    models.init_db()

models.py

from flask_sqlalchemy import SQLAlchemy
import logging as lg
from press_app.views import app

# Create database connection object
db = SQLAlchemy(app)

class Content(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    author = db.Column(db.String(100), nullable=True)
    content = db.Column(db.String(700), nullable=True)
    description = db.Column(db.String(700), nullable=True)
    publishedAt = db.Column(db.DateTime())
    source = db.Column(db.String(100), nullable=True)
    title = db.Column(db.String(200), nullable=True)
    url = db.Column(db.String(1000), nullable=True)
    urlToImage = db.Column(db.String(1000), nullable=True)

    def __init__(self, author, content, description, publishedAt, source, title, url, urlToImage):
        self.author = author
        self.content = content
        self.description = description
        self.publishedAt = publishedAt
        self.source = source
        self.title = title
        self.url = url
        self.urlToImage = urlToImage

db.create_all()

def init_db():
    db.drop_all()
    db.create_all()
    db.session.commit()
    lg.warning('Database initialized!')

views.py

from flask import Flask, render_template
from press_app.models import Content

app = Flask(__name__)

# Config options - Make sure you created a 'config.py' file.
app.config.from_object('config')
# To get one variable, tape app.config['MY_VARIABLE']

@app.route('/')
def index():
    last_article = Content.query.order_by(Content.id.desc()).first()
    last_id = last_article.id

    # Derniers titres
    title1 = Content.query.get(last_id).title
    title2 = Content.query.get(last_id - 1).title

    return render_template("index3.html", titre1 = title1)

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

config.py

import os

basedir = os.path.abspath(os.path.dirname(__file__))
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'app.db')

and run.py

from press_app import app
if __name__ == "__main__":
    app.run()

Because of a the following error message :

ImportError: cannot import name 'app' from partially initialized module 'press_app.views' (most likely due to a circular import) 

I choose to move one of my import in the views.py file directly in my function, as follow :

views.py, second version

from flask import Flask, render_template

app = Flask(__name__)

# Config options - Make sure you created a 'config.py' file.
app.config.from_object('config')
# To get one variable, tape app.config['MY_VARIABLE']

@app.route('/')
def index():
    from press_app.models import Content

    last_article = Content.query.order_by(Content.id.desc()).first()
    last_id = last_article.id

    # Derniers titres
    title1 = Content.query.get(last_id).title
    title2 = Content.query.get(last_id - 1).title

    return render_template("index3.html", titre1 = title1)

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

However, i'm facing a new error message (error 500) I can't resolve, despite I read many documentation and stackoverflow messages about it.

AssertionError: The sqlalchemy extension was not registered to the current application.  Please make sure to call init_app() first.

How can I solve this situation ?

Upvotes: 0

Views: 1752

Answers (1)

jen
jen

Reputation: 199

This issue is since you are initializing the models in another file and flask app in another, your app needs to be initialized before the sqlalchemy object, so that you can use it.

So your files should look like this.

views.py

from flask import Flask, render_template
from models import db

app = Flask(__name__)

# Config options - Make sure you created a 'config.py' file.
app.config.from_object('config')
# To get one variable, tape app.config['MY_VARIABLE']
db.init_app(app)
with app.app_context():
    db.create_all()
    db.session.commit()


@app.route('/')
def index():
    from models import Content

    last_article = Content.query.order_by(Content.id.desc()).first()
    last_id = last_article.id

    # Derniers titres
    title1 = Content.query.get(last_id).title
    title2 = Content.query.get(last_id - 1).title

    return render_template("index3.html", titre1 = title1)


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

models.py

from flask_sqlalchemy import SQLAlchemy
import logging as lg

# Create database connection object
db = SQLAlchemy()

class Content(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    author = db.Column(db.String(100), nullable=True)
    content = db.Column(db.String(700), nullable=True)
    description = db.Column(db.String(700), nullable=True)
    publishedAt = db.Column(db.DateTime())
    source = db.Column(db.String(100), nullable=True)
    title = db.Column(db.String(200), nullable=True)
    url = db.Column(db.String(1000), nullable=True)
    urlToImage = db.Column(db.String(1000), nullable=True)

    def __init__(self, author, content, description, publishedAt, source, title, url, urlToImage):
        self.author = author
        self.content = content
        self.description = description
        self.publishedAt = publishedAt
        self.source = source
        self.title = title
        self.url = url
        self.urlToImage = urlToImage


def init_db():
    db.drop_all()
    db.create_all()
    db.session.commit()
    lg.warning('Database initialized!')

Upvotes: 1

Related Questions