sahilmohile15 0 Light Poster

I have been trying to solve this error for almost 2 days have tried various posts from multiple places like github, stack overflow, and even daniweb. None have helped so far. So expecting atleast explaination for issue if you don't have answer.
So here how I am creating database in connections.py

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
#from app import app
from werkzeug.security import generate_password_hash, check_password_hash

app = Flask(__name__)

db = SQLAlchemy(app)

class wreport(db.Model):
    _id = db.Column("id", db.Integer, primary_key=True)
    tname = db.Column("teacher_name", db.String(255))

   def __init__(self,tname):
        self.tname = tname

and I am calling the class in app.py. There is no init.py
app.py

from flask import (Flask, request, redirect, render_template, url_for, session, blueprints, flash)
from flask_sqlalchemy import SQLAlchemy
import config

app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///report.sqlite3'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SECRET_KEY']= 'Teachers Report'
app.config.from_object(config.DevelopementConfig)

from connection import (db, users, wreport, dreport, students)

db.create_all()
db.session.commit()

#Example of how I am calling the class and trying to pass them
@app.route('/weekly', methods = ["POST", "GET"])
def weekly():
    if request.method == 'POST':
        wreports = wreport(request.form['tname'])
        db.session.add(wreports)
        db.session.commit()

#I am also passing posting the initialization section

db.init_app(app)

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

I tried calling db.create_all() from the connections.py but it doesn't work either. I am doing proper import of wreport class in app.py.

Dani AI

Generated

The error you're seeing is most often caused by how the app and the SQLAlchemy object are created and initialized. In the code shown by a Flask app is created inside connection.py and SQLAlchemy is instantiated against that app before the main app.py config runs. That leads to the extension being bound to the wrong app (or to no proper config/context) so db.create_all() or later db.session calls run against the wrong engine — producing OperationalError.

Fix (minimal, reliable pattern)

  • Make connection.py only hold db = SQLAlchemy() and the model classes (no Flask() there).
  • In app.py create and configure the Flask app, call db.init_app(app), then run schema creation inside an application context.
  • Avoid calling db.create_all() before db.init_app(app) or at import time.

Example pattern (illustrative — not a copy of your original files):

# connection.py
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class WReport(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    teacher_name = db.Column(db.String(255), nullable=False)
    def __init__(self, tname):
        self.teacher_name = tname
# app.py (after creating and configuring app)
from connection import db, WReport
db.init_app(app)
with app.app_context():
    db.create_all()

Quick troubleshooting checklist

  • If the error text says “no such table” then create_all() never ran under the correct app context. Move it after db.init_app(app) and into app.app_context().
  • If it says “attempt to write a readonly database” or “unable to open database file”, check the file path (relative path vs absolute) and file/directory permissions.
  • Check config.DevelopementConfig — it may override your DB URI if loaded after you set the URI.

Final notes
Avoid creating multiple Flask instances. For ongoing schema changes, use Flask-Migrate instead of repeated create_all().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.