本文整理汇总了Python中sqlalchemy.orm.subqueryload方法的典型用法代码示例。如果您正苦于以下问题:Python orm.subqueryload方法的具体用法?Python orm.subqueryload怎么用?Python orm.subqueryload使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.orm
的用法示例。
在下文中一共展示了orm.subqueryload方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_all_API_list
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import subqueryload [as 别名]
def get_all_API_list(self, session, pagination_helper=None, **filters):
"""Get a list of health monitors for the API list call.
This get_all returns a data set that is only one level deep
in the data graph. This is an optimized query for the API health
monitor list method.
:param session: A Sql Alchemy database session.
:param pagination_helper: Helper to apply pagination and sorting.
:param filters: Filters to decide which entities should be retrieved.
:returns: [octavia.common.data_model]
"""
# sub-query load the tables we need
# no-load (blank) the tables we don't need
query_options = (
subqueryload(models.HealthMonitor.pool),
subqueryload(models.HealthMonitor._tags),
noload('*'))
return super(HealthMonitorRepository, self).get_all(
session, pagination_helper=pagination_helper,
query_options=query_options, **filters)
示例2: with_subquery
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import subqueryload [as 别名]
def with_subquery(cls, *paths):
"""
Eagerload for simple cases where we need to just
joined load some relations
In strings syntax, you can split relations with dot
(it's SQLAlchemy feature)
:type paths: *List[str] | *List[InstrumentedAttribute]
Example 1:
User.with_subquery('posts', 'posts.comments').all()
Example 2:
User.with_subquery(User.posts, User.comments).all()
"""
options = [subqueryload(path) for path in paths]
return cls.query.options(*options)
示例3: show_entity
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import subqueryload [as 别名]
def show_entity(group, name):
entity = Entity.query.filter(Entity.group==group, Entity.name==name).first()
if not entity:
return make_response("The specified entity could not be found.", 404)
if entity.person:
return redirect(url_for('show_person', id=entity.person.id))
query = db.session.query(distinct(Document.id))\
.join(DocumentEntity)\
.filter(DocumentEntity.entity_id == entity.id)\
.order_by(Document.published_at.desc())
pagination = paginate(query, int(request.args.get('page', 1)), 50)
doc_ids = [r[0] for r in pagination.items]
docs = Document.query\
.options(subqueryload(Document.utterances))\
.filter(Document.id.in_(doc_ids))\
.order_by(Document.published_at.desc())\
.all()
return render_template('entities/show.haml', entity=entity, docs=docs, pagination=pagination)
示例4: trips
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import subqueryload [as 别名]
def trips(self, fltr=None, prefetch_stop_times=True, prefetch_routes=False, prefetch_stops=False, prefetch_calendars=False, batch_size=800):
idquery = self._session.query(Trip.feed_id, Trip.trip_id).distinct()
if fltr is not None:
idquery = _AutoJoiner(self._orm, idquery, fltr).autojoin()
idquery = idquery.filter(fltr)
# Only query IDs first
tripids = idquery.all()
def query_factory():
query = self._session.query(Trip)
_prefetch_stop_times = prefetch_stop_times
if prefetch_stops:
_prefetch_stop_times = True
if _prefetch_stop_times:
loadopt = subqueryload('stop_times')
if prefetch_stops:
loadopt = loadopt.subqueryload('stop')
query = query.options(loadopt)
if prefetch_routes:
query = query.options(subqueryload('route'))
if prefetch_calendars:
query = query.options(subqueryload('calendar'))
return query
return self._page_query(query_factory, Trip.feed_id, Trip.trip_id, tripids, batch_size)
示例5: stoptimes
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import subqueryload [as 别名]
def stoptimes(self, fltr=None, prefetch_trips=True, prefetch_stop_times=False):
query = self._session.query(StopTime).distinct()
if fltr is not None:
query = _AutoJoiner(self._orm, query, fltr).autojoin()
query = query.filter(fltr)
if prefetch_stop_times:
prefetch_trips = True
if prefetch_trips:
loadopt = subqueryload('trip')
if prefetch_stop_times:
loadopt = loadopt.subqueryload('stop_times')
query = query.options(loadopt)
# Note: ID batching would be difficult to implement for StopTime
# as StopTime do have a composite-primary composed of 3 elements
# and 2 of them (trip_id + stop_seq) can't be grouped easily.
return query.all()
示例6: fare_attributes
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import subqueryload [as 别名]
def fare_attributes(self, fltr=None, prefetch_fare_rules=True):
query = self._session.query(FareAttribute).distinct()
if fltr is not None:
# TODO _AutoJoiner will not work here
# The join / filter to deduce from a general filter expression
# is rather complex, as there are many paths from a fare attribute
# to any object. For example, 4 paths for an agency:
# Path 1: farerule-route-agency,
# Path 2/3/4: farerule-zone[origin,destination,contains]-stop-stoptime-trip-route-agency
# BUT we also sometimes need to include any fare_attributes containing rules that does
# not correspond to any entities, as they are the default...
# query = query.join(FareRule).join(Route, FareRule.route).filter(fltr)
query = query.filter(fltr)
if prefetch_fare_rules:
query = query.options(subqueryload('fare_rules'))
return query.all()
示例7: test_twolevel_subqueryload_wsubclass
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import subqueryload [as 别名]
def test_twolevel_subqueryload_wsubclass(self):
ParentThing, DataContainer, SubJob = (
self.classes.ParentThing,
self.classes.DataContainer,
self.classes.SubJob,
)
s = Session(testing.db)
q = s.query(ParentThing).options(
subqueryload(ParentThing.container).subqueryload(
DataContainer.jobs.of_type(SubJob)
)
)
def go():
eq_(q.all(), self._fixture())
self.assert_sql_count(testing.db, go, 7)
示例8: test_primary_eager_aliasing_joinedload
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import subqueryload [as 别名]
def test_primary_eager_aliasing_joinedload(self):
# For both joinedload() and subqueryload(), if the original q is
# not loading the subclass table, the joinedload doesn't happen.
sess = create_session()
def go():
eq_(
sess.query(Person)
.order_by(Person.person_id)
.options(joinedload(Engineer.machines))[1:3],
all_employees[1:3],
)
count = {"": 6, "Polymorphic": 3}.get(self.select_type, 4)
self.assert_sql_count(testing.db, go, count)
示例9: test_primary_eager_aliasing_subqueryload
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import subqueryload [as 别名]
def test_primary_eager_aliasing_subqueryload(self):
# test that subqueryload does not occur because the parent
# row cannot support it
sess = create_session()
def go():
eq_(
sess.query(Person)
.order_by(Person.person_id)
.options(subqueryload(Engineer.machines))
.all(),
all_employees,
)
count = {"": 14, "Polymorphic": 7}.get(self.select_type, 8)
self.assert_sql_count(testing.db, go, count)
示例10: test_subq_through_related
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import subqueryload [as 别名]
def test_subq_through_related(self):
Parent = self.classes.Parent
Base = self.classes.Base
sess = Session()
def go():
eq_(
sess.query(Parent)
.options(
subqueryload(Parent.children).subqueryload(Base.related)
)
.order_by(Parent.data)
.all(),
[p1, p2],
)
self.assert_sql_count(testing.db, go, 3)
示例11: test_subq_through_related_aliased
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import subqueryload [as 别名]
def test_subq_through_related_aliased(self):
Parent = self.classes.Parent
Base = self.classes.Base
pa = aliased(Parent)
sess = Session()
def go():
eq_(
sess.query(pa)
.options(subqueryload(pa.children).subqueryload(Base.related))
.order_by(pa.data)
.all(),
[p1, p2],
)
self.assert_sql_count(testing.db, go, 3)
示例12: test_subqueryload
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import subqueryload [as 别名]
def test_subqueryload(self):
Subparent = self.classes.Subparent
sess = create_session()
def go():
eq_(
sess.query(Subparent)
.options(subqueryload(Subparent.children))
.all(),
[p1, p2],
)
self.assert_sql_count(testing.db, go, 2)
sess.expunge_all()
def go():
eq_(
sess.query(Subparent).options(subqueryload("children")).all(),
[p1, p2],
)
self.assert_sql_count(testing.db, go, 2)
示例13: test_free_w_poly_subquery
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import subqueryload [as 别名]
def test_free_w_poly_subquery(self):
A = self.classes.A
B = self.classes.B
C = self.classes.C
D = self.classes.D
session = Session()
d = session.query(D).one()
a_poly = with_polymorphic(A, [B, C])
def go():
for a in session.query(a_poly).options(
subqueryload(a_poly.B.related), subqueryload(a_poly.C.related)
):
eq_(a.related, [d])
self.assert_sql_count(testing.db, go, 3)
示例14: test_uselist_false_warning
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import subqueryload [as 别名]
def test_uselist_false_warning(self):
"""test that multiple rows received by a
uselist=False raises a warning."""
User, users, orders, Order = (
self.classes.User,
self.tables.users,
self.tables.orders,
self.classes.Order,
)
mapper(
User,
users,
properties={"order": relationship(Order, uselist=False)},
)
mapper(Order, orders)
s = create_session()
assert_raises(
sa.exc.SAWarning,
s.query(User).options(subqueryload(User.order)).all,
)
示例15: test_loads_second_level_collection_to_scalar
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import subqueryload [as 别名]
def test_loads_second_level_collection_to_scalar(self):
User, Address, Dingaling, sess = self._collection_to_scalar_fixture()
u1 = sess.query(User).get(8)
a1 = Address()
u1.addresses.append(a1)
a2 = u1.addresses[0]
a2.email_address = "foo"
sess.query(User).options(
subqueryload("addresses").subqueryload("dingaling")
).filter_by(id=8).all()
assert u1.addresses[-1] is a1
for a in u1.addresses:
if a is not a1:
assert "dingaling" in a.__dict__
else:
assert "dingaling" not in a.__dict__
if a is a2:
eq_(a2.email_address, "foo")