本文整理汇总了Python中sqlalchemy.orm.deferred方法的典型用法代码示例。如果您正苦于以下问题:Python orm.deferred方法的具体用法?Python orm.deferred怎么用?Python orm.deferred使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.orm
的用法示例。
在下文中一共展示了orm.deferred方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_unsaved
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import deferred [as 别名]
def test_unsaved(self):
"""Deferred loading does not kick in when just PK cols are set."""
Order, orders = self.classes.Order, self.tables.orders
mapper(
Order,
orders,
properties={"description": deferred(orders.c.description)},
)
sess = create_session()
o = Order()
sess.add(o)
o.id = 7
def go():
o.description = "some description"
self.sql_count_(0, go)
示例2: test_unsaved_group
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import deferred [as 别名]
def test_unsaved_group(self):
"""Deferred loading doesn't kick in when just PK cols are set"""
orders, Order = self.tables.orders, self.classes.Order
mapper(
Order,
orders,
properties=dict(
description=deferred(orders.c.description, group="primary"),
opened=deferred(orders.c.isopen, group="primary"),
),
)
sess = create_session()
o = Order()
sess.add(o)
o.id = 7
def go():
o.description = "some description"
self.sql_count_(0, go)
示例3: test_unsaved_group_2
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import deferred [as 别名]
def test_unsaved_group_2(self):
orders, Order = self.tables.orders, self.classes.Order
mapper(
Order,
orders,
properties=dict(
description=deferred(orders.c.description, group="primary"),
opened=deferred(orders.c.isopen, group="primary"),
),
)
sess = create_session()
o = Order()
sess.add(o)
def go():
o.description = "some description"
self.sql_count_(0, go)
示例4: test_raise_on_col_rowproc_only
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import deferred [as 别名]
def test_raise_on_col_rowproc_only(self):
orders, Order = self.tables.orders, self.classes.Order
mapper(
Order,
orders,
properties={
"description": deferred(orders.c.description, raiseload=True)
},
)
sess = create_session()
stmt = sa.select([Order]).order_by(Order.id)
o1 = (sess.query(Order).from_statement(stmt).all())[0]
assert_raises_message(
sa.exc.InvalidRequestError,
"'Order.description' is not available due to raiseload=True",
getattr,
o1,
"description",
)
示例5: test_load_only_w_deferred
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import deferred [as 别名]
def test_load_only_w_deferred(self):
orders, Order = self.tables.orders, self.classes.Order
mapper(
Order,
orders,
properties={"description": deferred(orders.c.description)},
)
sess = create_session()
q = sess.query(Order).options(
load_only("isopen", "description"), undefer("user_id")
)
self.assert_compile(
q,
"SELECT orders.description AS orders_description, "
"orders.id AS orders_id, "
"orders.user_id AS orders_user_id, "
"orders.isopen AS orders_isopen FROM orders",
)
示例6: setup_classes
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import deferred [as 别名]
def setup_classes(cls):
Base = cls.DeclarativeBasic
class A(Base):
__tablename__ = "a"
id = Column(Integer, primary_key=True)
bs = relationship("B")
class B(Base):
__tablename__ = "b"
id = Column(Integer, primary_key=True)
a_id = Column(ForeignKey("a.id"))
A.b_count = deferred(
select([func.count(1)]).where(A.id == B.a_id).scalar_subquery()
)
示例7: test_deferred_notfound
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import deferred [as 别名]
def test_deferred_notfound(self):
users, User = self.tables.users, self.classes.User
mapper(User, users, properties={"name": deferred(users.c.name)})
s = create_session(autocommit=False)
u = s.query(User).get(10)
assert "name" not in u.__dict__
s.execute(users.delete().where(User.id == 10))
assert_raises_message(
sa.orm.exc.ObjectDeletedError,
"Instance '<User at .*?>' has been "
"deleted, or its row is otherwise not present.",
getattr,
u,
"name",
)
示例8: setup_mappers
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import deferred [as 别名]
def setup_mappers(cls):
global WeatherLocation, Report
class WeatherLocation(object):
def __init__(self, continent, city):
self.continent = continent
self.city = city
class Report(object):
def __init__(self, temperature, id_=None):
self.temperature = temperature
if id_:
self.id = id_
mapper(
WeatherLocation,
weather_locations,
properties={
"reports": relationship(Report, backref="location"),
"city": deferred(weather_locations.c.city),
},
)
mapper(Report, weather_reports)
示例9: test_deferred
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import deferred [as 别名]
def test_deferred(self):
class User(Base, fixtures.ComparableEntity):
__tablename__ = "users"
id = Column(
Integer, primary_key=True, test_needs_autoincrement=True
)
name = sa.orm.deferred(Column(String(50)))
Base.metadata.create_all()
sess = create_session()
sess.add(User(name="u1"))
sess.flush()
sess.expunge_all()
u1 = sess.query(User).filter(User.name == "u1").one()
assert "name" not in u1.__dict__
def go():
eq_(u1.name, "u1")
self.assert_sql_count(testing.db, go, 1)
示例10: test_add_deferred
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import deferred [as 别名]
def test_add_deferred(self):
class Person(Base, fixtures.ComparableEntity):
__tablename__ = "people"
id = Column(
"id", Integer, primary_key=True, test_needs_autoincrement=True
)
Person.name = deferred(Column(String(10)))
Base.metadata.create_all()
sess = create_session()
p = Person(name="ratbert")
sess.add(p)
sess.flush()
sess.expunge_all()
eq_(sess.query(Person).all(), [Person(name="ratbert")])
sess.expunge_all()
person = sess.query(Person).filter(Person.name == "ratbert").one()
assert "name" not in person.__dict__
示例11: make_geom_lazy
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import deferred [as 别名]
def make_geom_lazy(cls):
from sqlalchemy.orm import deferred
try:
cls.__mapper__.add_property('geom', deferred(cls.__table__.c.geom))
except Exception as e:
log.warning(e)
示例12: add_geometry_column
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import deferred [as 别名]
def add_geometry_column(cls):
if not hasattr(cls, 'geom'):
from geoalchemy2 import Geometry
cls.geom = deferred(Column(Geometry('MULTILINESTRING')))
示例13: add_geometry_column
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import deferred [as 别名]
def add_geometry_column(cls):
if not hasattr(cls, 'geom'):
cls.geom = deferred(Column(Geometry(geometry_type='LINESTRING', srid=config.SRID)))
示例14: test_deferred
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import deferred [as 别名]
def test_deferred(self):
"""test versioning of unloaded, deferred columns."""
class SomeClass(Versioned, self.Base, ComparableEntity):
__tablename__ = "sometable"
id = Column(Integer, primary_key=True)
name = Column(String(50))
data = deferred(Column(String(25)))
self.create_tables()
sess = self.session
sc = SomeClass(name="sc1", data="somedata")
sess.add(sc)
sess.commit()
sess.close()
sc = sess.query(SomeClass).first()
assert "data" not in sc.__dict__
sc.name = "sc1modified"
sess.commit()
assert sc.version == 2
SomeClassHistory = SomeClass.__history_mapper__.class_
eq_(
sess.query(SomeClassHistory)
.filter(SomeClassHistory.version == 1)
.all(),
[SomeClassHistory(version=1, name="sc1", data="somedata")],
)
示例15: test_basic
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import deferred [as 别名]
def test_basic(self):
"""A basic deferred load."""
Order, orders = self.classes.Order, self.tables.orders
mapper(
Order,
orders,
properties={"description": deferred(orders.c.description)},
)
o = Order()
self.assert_(o.description is None)
q = create_session().query(Order).order_by(Order.id)
def go():
result = q.all()
o2 = result[2]
o2.description
self.sql_eq_(
go,
[
(
"SELECT orders.id AS orders_id, "
"orders.user_id AS orders_user_id, "
"orders.address_id AS orders_address_id, "
"orders.isopen AS orders_isopen "
"FROM orders ORDER BY orders.id",
{},
),
(
"SELECT orders.description AS orders_description "
"FROM orders WHERE orders.id = :param_1",
{"param_1": 3},
),
],
)