Say I have a model in my Flask application like this:
class Foo(db.Model):
id = db.Column(db.Integer, primary_key=True)
bar = db.Column(db.String)
In a Flask view, I want to query my database to retrieve a record of this type.
In tutorials I've seen people run such a query in two ways: using the flask.ext.sqlalchemy.SQLAlchemy
configuration or the model.
Using flask.ext.sqlalchemy.SQLAlchemy
The app is set up something like this ...
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config.from_object(os.environ['MY_SETTINGS'])
db = SQLAlchemy(app)
Then the query in the view looks like this ...
my_query = db.session.query(Foo).filter_by(id=some_id)
Using the model
The other way I've seen the query executed is using the model, like this ...
import Foo
my_query = Foo.query.filter_by(id=some_id)
What's the difference?
As far as I can tell, the query returns exactly the same thing. So it's hard for me to tell which method I should be using. Or does it make no difference?
What about when it comes to creating and deleting records?