E
E
Elephantsdadshusband2019-01-16 14:10:16
Flask
Elephantsdadshusband, 2019-01-16 14:10:16

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:

Project structure
5c3f0e8e9f528923080415.png

chat.py file:
from app import create_app, socketio
app = create_app(debug=True)
if __name__ == '__main__':
    socketio.run(app)

app\__init__.py file:
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

app\main\__init__.py:
from flask import Blueprint
main = Blueprint('main', __name__)
from . import routes, events

app\models.py:
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}')"

Through the terminal I try to connect to the database and add an entry with the following set of commands:
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)

Gives an error message
RuntimeError: No application found. Either work inside a view function or push an application

There was nothing in the documentation that would help, nothing on other q / a sites either. Solutions to similar problems do not help. Any ideas how to solve this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
aderes, 2019-01-16
@de-iure

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 question

Ask a Question

731 491 924 answers to any question