Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between Model.query and session.query(Model) in SQLAlchemy?

I'm a beginner in SQLAlchemy and found query can be done in 2 method:

Approach 1:

DBSession = scoped_session(sessionmaker()) class _Base(object):     query = DBSession.query_property()  Base = declarative_base(cls=_Base)  class SomeModel(Base):     key   = Column(Unicode, primary_key=True)     value = Column(Unicode)  # When querying result = SomeModel.query.filter(...) 

Approach 2

DBSession = scoped_session(sessionmaker()) Base = declarative_base()  class SomeModel(Base):     key   = Column(Unicode, primary_key=True)     value = Column(Unicode)  # When querying session = DBSession() result = session.query(SomeModel).filter(...) 

Is there any difference between them?

like image 828
Cosmia Luna Avatar asked Sep 10 '12 11:09

Cosmia Luna


People also ask

What does Session do in SQLAlchemy?

What does the Session do? One of the core concepts in SQLAlchemy is the Session . A Session establishes and maintains all conversations between your program and the databases. It represents an intermediary zone for all the Python model objects you have loaded in it.

What is a model SQLAlchemy?

Model is a class within the Flask-SQLAlchemy project. Flask-SQLAlchemy makes it easier to use SQLAlchemy within a Flask application. SQLAlchemy is used to read, write, query and delete persistent data in a relational database through SQL or the object-relational mapper (ORM) interface built into the project.

What does DB Session query do?

It provides the interface where SELECT and other queries are made that will return and modify ORM-mapped objects.

What function from the Session object is used to delete items in SQLAlchemy?

delete() is invoked upon an object and the Session is flushed, the row is deleted from the database.


2 Answers

In the code above, there is no difference. This is because, in line 3 of the first example:

  • the query property is explicitly bound to DBSession
  • there is no custom Query object passed to query_property

As @petr-viktorin points out in the answer here, there must be a session available before you define your model in the first example, which might be problematic depending on the structure of your application.

If, however, you need a custom query that adds additional query parameters automatically to all queries, then only the first example will allow that. A custom query class that inherits from sqlalchemy.orm.query.Query can be passed as an argument to query_property. This question shows an example of that pattern.

Even if a model object has a custom query property defined on it, that property is not used when querying with session.query, as in the last line in the second example. This means something like the first example the only option if you need a custom query class.

like image 120
glyphobet Avatar answered Sep 23 '22 01:09

glyphobet


An answer (here) to a different SQLAlchemy question might help. That answer starts with:

You can use Model.query, because the Model (or usually its base class, especially in cases where declarative extension is used) is assigned Session.query_property. In this case the Model.query is equivalent to Session.query(Model).

like image 45
Robert Avatar answered Sep 24 '22 01:09

Robert