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

Recommended way to setup SQLAlchemy #13

Closed
fraunos opened this issue Aug 7, 2018 · 2 comments
Closed

Recommended way to setup SQLAlchemy #13

fraunos opened this issue Aug 7, 2018 · 2 comments

Comments

@fraunos
Copy link

fraunos commented Aug 7, 2018

Hi,
I'm new to Python and Flask - I'm trying to setup a db connection in a project based on this template, which is quite problematic for me. I'm following some tutorials, but some are outdated and most show only very simple setups. (http://exploreflask.com/en/latest/storing.html http://flask-sqlalchemy.pocoo.org/2.3/)

While trying to use current_app imported from flask in a db.py file based in app/api I get

RuntimeError: Working outside of application context.

This typically means that you attempted to use functionality that needed
to interface with the current application object in a way.  To solve
this set up an application context with app.app_context().  See the
documentation for more information.

Currently the error I'm facing is -

/home/michalw/dev/python/venv/bin/python: Error while finding module specification for 'app.__main__' (ImportError: cannot import name 'db' from 'app' (/home/michalw/dev/python/flask-vuejs-template/app/__init__.py))

I imported and initialized flask_sqlalchemy in app/__init__.py.

So I've got following questions:

  • which file should SQLAlchemy be imported in and db object initialized
  • how to use db object outside this file to declare models classes
  • should I use Blueprint for that? How?
@gtalarico
Copy link
Owner

gtalarico commented Aug 7, 2018

Hi @fraunos -
Your question is complex and probably better suited for a stack overflow post rather than an issue on this repository. I am closing this since it's not related to the template.

But since I remember the pain of running into these issue as if it was yesterday,
I will add some thoughts and links, hopefully they will help you

RuntimeError: Working outside of application context.

It's important to understand application context and request context when working with flask.
You should absolutely watch this video from Miguel Grindberg:
https://blog.miguelgrinberg.com/post/flask-webcast-2-request-and-application-contexts

After watching the video these should be very easy to understand:
http://flask-sqlalchemy.pocoo.org/2.3/contexts/
http://flask.pocoo.org/docs/1.0/appcontext/#creating-an-application-context

cannot import name 'db' from 'app'

It's hard to say anything about this without seeing the whole repo, but from the sound of it it's probably a circular import issue, like this one

I would suggest you also see this video from Miguel:
Flask Circular Imports

which file should SQLAlchemy be imported in and db object initialized
how to use db object outside this file to declare models classes

There are many examples out in the wild of how this is usually done.
Just pick one that feels right to you.

The only weird thing is that sometimes an import needs to go at the end of the file to avoid cicular imports. Miguells' video on imports above goes deep into to the issue and will help you understand and decide how to best organize your app.

Other Examples:
Example 1
Example 2

should I use Blueprint for that? How?

Up to you.
My thoughts on blueprints is to avoid them until they are needed....
You will know when you need them

@fraunos
Copy link
Author

fraunos commented Aug 10, 2018

Thanks a lot, it helped me clarify these problems

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

2 participants