2

I would like to add data to a database with Flask-SQLAlchemy without the Flask app running.

Is there a way to get db back from the app after the app and the database have been initialized.

My code looks like

db = SQLAlchemy()

def init_app():

    app = Flask(__name__)

    db = SQLAlchemy(app)
    db.init_app(app)

    return app

And what I would like to do is something like

from app import init_app
app = init_app() # initialized but not running

# db is used in model.py, but not initialized
# with Flask
# from db = SQLAlchemy()
from model import Machine # class Machine(db.Model)

p = Machine(name='something')

# now I need the initialized db from somewhere
db.session.add(p)
db.session.commit()   

Basically I would like to do what's described here:

Another disadvantage is that Flask-SQLAlchemy makes using the database outside of a Flask context difficult. This is because, with FLask-SQLAlchemy, the database connection, models, and app are all located within the app.py file. Having models within the app file, we have limited ability to interact with the database outside of the app. This makes loading data outside of your app difficult. Additionally, this makes it hard to retrieve data outside of the Flask context.

Joe
  • 6,758
  • 2
  • 26
  • 47
  • One of the possible solutions: [Using SQLAlchemy models in and out of Flask](https://stackoverflow.com/a/41014157/6682517) – Sergey Shubin Oct 09 '20 at 12:05
  • Yes, that would work. But I don't want to give up the comfort from Flask-SQLAlchemy. Using that solution is basically redundant and you could skip Flask-SQLAlchemy at all. – Joe Oct 09 '20 at 12:21

2 Answers2

0

Well, once you initialize the app, Flask spines a server (either development or production, whichever you set), so if you would like to add data to a database with Flask-SQLAlchemy without the Flask app running, you would better use the flask shell command which runs in the context of the current app, then you could add your data.

But first, it would be better is you set up your app as the following so we could directly import stuff like db, auth, etc:

...
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

def init_app():

    app = Flask(__name__)
    db.init_app(app)

    return app

In the root of your project, type in the terminal the following command

flask shell

Now that you have a shell running in the context of the current app but not the server not running:

from app import db

from model import Machine # class Machine(db.Model)

p = Machine(name='something')

# now I need the initialized db from somewhere
db.session.add(p)
db.session.commit() 

coredumped0x
  • 768
  • 1
  • 12
  • 28
0

From the wonderful tutorial...

https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-iv-database

Define something like this...

from app import app, db
from app.models import User, Post

@app.shell_context_processor
def make_shell_context():
    return {'db': db, 'User': User, 'Post': Post}

And then...

(venv) $ flask shell
>>> db
<SQLAlchemy engine=sqlite:////Users/migu7781/Documents/dev/flask/microblog2/app.db>
>>> User
<class 'app.models.User'>
>>> Post
<class 'app.models.Post'>
Jürgen Gmach
  • 5,366
  • 3
  • 20
  • 37