Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flask-shell-bpython interferes with flask-sqlalchemy inside flask shell #13

Open
bbbart opened this issue Jan 19, 2019 · 6 comments
Open

Comments

@bbbart
Copy link

bbbart commented Jan 19, 2019

Please have a look at my Stackoverflow question at https://stackoverflow.com/questions/54268271/sqlalchemy-db-session-commit-behaviour. I'll copy paste it here as reference too:

I'm completely stupefied by the following issue I'm having.

In a flask app with flask_sqlalchemy I'm quite happy creating my model in Python. I have largely the same structure as Miguel Grinberg uses in his Flask Megatutorial.

However, when trying to manually add stuff into the databse (sqlite3) via flask shell, creating some objects, adding them to db.session and then finishing with db.session.commit() doesn't work. No errors, no warnings, no complaints, but also no data in the database, nor in the session itself.

However, when I add a breakpoint() at the bottom of app/init.py, then run flask shell, open a shell again from inside the debugger (I'm using pudb) and then perform the exact same commands as described above, I am getting data in the session and the database.

Smells like a Heisenbug, but it's not so funny a concept when you're confronted with it yourself. :-)

Does anybody have a clue where to start looking for a solution here? I have tried with sqlite://:memory: and with sqlite files at various paths. Digging further into the app, db and db.session objects from pudb didn't teach me anything useful till now.

Thanks for any pointers!

EDIT: I finally found out what was causing the problem: flask-shell-bpython. Simply uninstalling this package, returning the standard REPL to flask shell, solved the issue explained above.

The fact that it did work from the shell launched from pudb, made me try this out (even though pudb is configured to also launch bpython, so it's not the REPL itself that is at fault here).

@jacquerie
Copy link
Owner

jacquerie commented Jan 20, 2019

Thanks for the report! I see something different, a loud error:

>>> from app.models import Employee, db
>>> employee = Employee('[email protected]', 'password')
>>> db.session.add(employee)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    db.session.add(employee)
  File "/home/jacquerie/.envs/contoso-ace/lib/python3.6/site-packages/sqlalchemy/orm/scoping.py", lin
e 153, in do
    return getattr(self.registry(), name)(*args, **kwargs)
  File "/home/jacquerie/.envs/contoso-ace/lib/python3.6/site-packages/sqlalchemy/util/_collections.py
", line 1001, in __call__
    return self.registry.setdefault(key, self.createfunc())
  File "/home/jacquerie/.envs/contoso-ace/lib/python3.6/site-packages/sqlalchemy/orm/session.py", lin
e 3010, in __call__
    return self.class_(**local_kw)
  File "/home/jacquerie/.envs/contoso-ace/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", 
line 141, in __init__
    self.app = app = db.get_app()
  File "/home/jacquerie/.envs/contoso-ace/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", 
line 912, in get_app
    'No application found. Either work inside a view function or push'
RuntimeError: No application found. Either work inside a view function or push an application context
. See http://flask-sqlalchemy.pocoo.org/contexts/.

In order to make it work I have to push an application context on the stack myself (which shouldn't be the case, so something is broken):

>>> with app.app_context():
...     employee = Employee('[email protected]', 'password')
...     db.session.add(employee)
...     db.session.commit()
>>>

Can you share a simplified version of your application? I'd like to reproduce the same (lack of) error...

@bbbart
Copy link
Author

bbbart commented Jun 18, 2019

hi!

Sorry, I totally forgot about this. Meanwhile my app has grown significantly, and I now have the same problem as you are experiencing. Big fat error first and everything working as expected after pushing the application context. Strange... :-/

1 similar comment
@bbbart
Copy link
Author

bbbart commented Jun 18, 2019

hi!

Sorry, I totally forgot about this. Meanwhile my app has grown significantly, and I now have the same problem as you are experiencing. Big fat error first and everything working as expected after pushing the application context. Strange... :-/

@bbbart bbbart closed this as completed Jun 18, 2019
@bbbart bbbart reopened this Jun 18, 2019
@bbbart
Copy link
Author

bbbart commented Jun 18, 2019

Sorry, maybe I should keep it open? My personal issue seems to have vanished, but of course the fact that the application context is not getting pushed of course goes against the Flask documentation of flask shell...

@maxnordlund
Copy link

Same issue here:

RuntimeError: No application found. Either work inside a view function or push an application context. See http://flask-sqlalchemy.pocoo.org/contexts/.

No idea why though 😞

@ongopongo
Copy link

ongopongo commented Sep 18, 2020

Same issue here.

Here's the traceback I got (analogous to @jacquerie's above):

>>> from factories import UserFactory
>>> from app import db
>>> users = UserFactory.create_batch(10)
>>> db.session.add_all(users) # error
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    db.session.add_all(users)
  File "/home/foo/venvs/venv_1/lib/python3.6/site-packages/sqlalchemy/orm/scoping.py", line 162, in do
    return getattr(self.registry(), name)(*args, **kwargs)
  File "/home/foo/venvs/venv_1/lib/python3.6/site-packages/sqlalchemy/util/_collections.py", line 1032, in __call__
    return self.registry.setdefault(key, self.createfunc())
  File "/home/foo/venvs/venv_1/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 3234, in __call__
    return self.class_(**local_kw)
  File "/home/foo/venvs/venv_1/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 136, in __init__
    self.app = app = db.get_app()
  File "/home/foo/venvs/venv_1/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 982, in get_app
    'No application found. Either work inside a view function or push'

The with app.app_context(): version shared above works for me, too.

>>> with app.app_context():
...     users = UserFactory.create_batch(10)
...     db.session.add_all(users) # no error
...     db.session.commit()
...
>>> 

For comparison, here's a working example using the normal Flask shell:

>>> from factories import UserFactory
>>> from app import db
>>> user = UserFactory.create()
>>> db.session.add(user) # no error
>>> db.session.commit()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants