Answer the question
In order to leave comments, you need to log in
How to properly connect sqlite database to flask application using SQLAlchemy?
There is a project on flask, we are trying to connect a database to it, but errors occur. About the project itself:
from app import create_app, socketio
app = create_app(debug=True)
if __name__ == '__main__':
socketio.run(app)
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_socketio import SocketIO
socketio = SocketIO()
db = SQLAlchemy()
def create_app(debug=False):
"""Create an application."""
app = Flask(__name__)
app.debug = debug
app.config['SECRET_KEY'] = 'gjr39dkjn344_!67#'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
db.init_app(app)
socketio.init_app(app)
from .main import main as main_blueprint
app.register_blueprint(main_blueprint)
return app
from flask import Blueprint
main = Blueprint('main', __name__)
from . import routes, events
from datetime import datetime
from app import db
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(15), nullable=False)
session_id = db.Column(db.String(200), nullable=False)
isready = db.Column(db.Boolean, default='False')
room = db.relationship('Rooms', backref='spy')
def __repr__(self):
return f"User('{self.username}')"
class Location(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(15), nullable=False)
image_file = db.Column(db.String(20))
rooms = db.relationship('Rooms', backref='place')
def __repr__(self):
return f"Location('{self.name}', '{self.image_file}')"
chats = db.Table('chats',
db.Column('room_id', db.Integer, db.ForeignKey('rooms.id')),
db.Column('user_id', db.Integer, db.ForeignKey('user.id'))
)
class Rooms(db.Model):
id = db.Column(db.Integer, primary_key=True)
s_id = db.Column(db.String(200), nullable=False)
status = db.Column(db.String(20), nullable=False)
start_time = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
location_id = db.Column(db.Integer, db.ForeignKey('location.id'))
spy_id = db.Column(db.Integer, db.ForeignKey('user.id'))
players = db.relationship('User', secondary=chats, backref=db.backref('chatroom'))
def __repr__(self):
return f"Rooms('{self.location}', '{self.spy}')"
from app import db, create_app
db.create_all(app=create_app())
from app.models import User, Rooms, Location, chats
loc_1 = Location(name='Музей', image_file='location 1.png')
db.session.add(loc_1)
RuntimeError: No application found. Either work inside a view function or push an application
Answer the question
In order to leave comments, you need to log in
look here: https://habr.com/ru/post/346344/
and you apparently did not migrate the database, there is no site.db file in the structure, and the path to the database is strange ... in general, read the link , I think everything will work out, you have nothing complicated (additionally see topic 15)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question