本文整理汇总了Python中sqlalchemy.orm.mapper函数的典型用法代码示例。如果您正苦于以下问题:Python mapper函数的具体用法?Python mapper怎么用?Python mapper使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了mapper函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setUp
def setUp(self):
super(TestBlog, self).setUp()
stats = self.stats = Table('stats', self.meta,
Column( 'date', Date, primary_key=True),
Column( 'posts_so_far', Integer),
)
blog = self.blog = Table('blog', self.meta,
Column( 'id', Integer, primary_key=True),
Column( 'date', Date, index=True),
Column( 'text', Text),
)
self.meta.create_all()
class BlogEntry(self.EasyInit):
pass
class StatRow(self.EasyInit):
pass
self.BlogEntry = BlogEntry
self.StatRow = StatRow
self.blog_mapper = mapper(BlogEntry, blog,
extension=self.aggregator_class(
a.Count(stats.c.posts_so_far, stats.c.date >= blog.c.date),
))
self.stats_mapper = mapper(StatRow, stats)
示例2: test_relationship
def test_relationship(self):
pjoin = polymorphic_union(
{"manager": managers_table, "engineer": engineers_table},
"type",
"pjoin",
)
mapper(
Company,
companies,
properties={"employees": relationship(Employee)},
)
employee_mapper = mapper(Employee, pjoin, polymorphic_on=pjoin.c.type)
manager_mapper = mapper(
Manager,
managers_table,
inherits=employee_mapper,
concrete=True,
polymorphic_identity="manager",
)
engineer_mapper = mapper(
Engineer,
engineers_table,
inherits=employee_mapper,
concrete=True,
polymorphic_identity="engineer",
)
session = create_session()
c = Company()
c.employees.append(Manager("Tom", "knows how to manage things"))
c.employees.append(Engineer("Kurt", "knows how to hack"))
session.add(c)
session.flush()
session.expunge_all()
def go():
c2 = session.query(Company).get(c.id)
assert set([repr(x) for x in c2.employees]) == set(
[
"Engineer Kurt knows how to hack",
"Manager Tom knows how to manage things",
]
)
self.assert_sql_count(testing.db, go, 2)
session.expunge_all()
def go():
c2 = (
session.query(Company)
.options(joinedload(Company.employees))
.get(c.id)
)
assert set([repr(x) for x in c2.employees]) == set(
[
"Engineer Kurt knows how to hack",
"Manager Tom knows how to manage things",
]
)
self.assert_sql_count(testing.db, go, 1)
示例3: test_get_pk_w_null
def test_get_pk_w_null(self):
"""test the re-implementation of logic to do get with IS NULL."""
class AddressUser(object):
pass
mapper(
AddressUser,
self.tables.users.outerjoin(self.tables.addresses),
properties={
"id": self.tables.users.c.id,
"address_id": self.tables.addresses.c.id
}
)
bq = self.bakery(lambda s: s.query(AddressUser))
sess = Session()
def go():
u1 = bq(sess).get((10, None))
eq_(u1.name, 'chuck')
self.assert_sql_count(testing.db, go, 1)
u1 = sess.query(AddressUser).get((10, None)) # noqa
def go():
u2 = bq(sess).get((10, None))
eq_(u2.name, 'chuck')
self.assert_sql_count(testing.db, go, 0)
示例4: test_locates_col_rowproc_only
def test_locates_col_rowproc_only(self):
"""changed in 1.0 - we don't search for deferred cols in the result
now.
Because the loading for ORM Query and Query from a core select
is now split off, we test loading from a plain select()
separately.
"""
orders, Order = self.tables.orders, self.classes.Order
mapper(Order, orders, properties={
'description': deferred(orders.c.description)})
sess = create_session()
stmt = sa.select([Order]).order_by(Order.id)
o1 = (sess.query(Order).
from_statement(stmt).all())[0]
def go():
eq_(o1.description, 'order 1')
# prior to 1.0 we'd search in the result for this column
# self.sql_count_(0, go)
self.sql_count_(1, go)
示例5: _setup_mappers
def _setup_mappers(self, tables, mappers):
mappers['test_users'] = orm.mapper(User, tables['test_users'],
properties={
'skills': orm.relation(Skill,
primaryjoin=tables['test_users'].columns['id'] == tables['test_skills'].columns['user_id']),
})
mappers['test_skills'] = orm.mapper(Skill, tables['test_skills'])
示例6: go
def go():
class A(fixtures.ComparableEntity):
pass
class B(A):
pass
mapper(
A,
table1,
polymorphic_on=table1.c.col2,
polymorphic_identity="a",
)
mapper(B, table2, inherits=A, polymorphic_identity="b")
sess = create_session()
a1 = A()
a2 = A()
b1 = B(col3="b1")
b2 = B(col3="b2")
for x in [a1, a2, b1, b2]:
sess.add(x)
sess.flush()
sess.expunge_all()
alist = sess.query(A).order_by(A.col1).all()
eq_([A(), A(), B(col3="b1"), B(col3="b2")], alist)
for a in alist:
sess.delete(a)
sess.flush()
# don't need to clear_mappers()
del B
del A
示例7: test_options
def test_options(self):
"""Options on a mapper to create deferred and undeferred columns"""
orders, Order = self.tables.orders, self.classes.Order
mapper(Order, orders)
sess = create_session()
q = sess.query(Order).order_by(Order.id).options(defer('user_id'))
def go():
q.all()[0].user_id
self.sql_eq_(go, [
("SELECT orders.id AS orders_id, "
"orders.address_id AS orders_address_id, "
"orders.description AS orders_description, "
"orders.isopen AS orders_isopen "
"FROM orders ORDER BY orders.id", {}),
("SELECT orders.user_id AS orders_user_id "
"FROM orders WHERE orders.id = :param_1",
{'param_1':1})])
sess.expunge_all()
q2 = q.options(undefer('user_id'))
self.sql_eq_(q2.all, [
("SELECT orders.id AS orders_id, "
"orders.user_id AS orders_user_id, "
"orders.address_id AS orders_address_id, "
"orders.description AS orders_description, "
"orders.isopen AS orders_isopen "
"FROM orders ORDER BY orders.id",
{})])
示例8: test_explicit_expunge_deleted
def test_explicit_expunge_deleted(self):
users, User = self.tables.users, self.classes.User
mapper(User, users)
sess = Session()
sess.add(User(name='x'))
sess.commit()
u1 = sess.query(User).first()
sess.delete(u1)
sess.flush()
assert was_deleted(u1)
assert u1 not in sess
assert object_session(u1) is sess
sess.expunge(u1)
assert was_deleted(u1)
assert u1 not in sess
assert object_session(u1) is None
sess.rollback()
assert was_deleted(u1)
assert u1 not in sess
assert object_session(u1) is None
示例9: test_autoflush_rollback
def test_autoflush_rollback(self):
Address, addresses, users, User = (self.classes.Address,
self.tables.addresses,
self.tables.users,
self.classes.User)
mapper(Address, addresses)
mapper(User, users, properties={
'addresses': relationship(Address)})
sess = create_session(autocommit=False, autoflush=True)
u = sess.query(User).get(8)
newad = Address(email_address='a new address')
u.addresses.append(newad)
u.name = 'some new name'
assert u.name == 'some new name'
assert len(u.addresses) == 4
assert newad in u.addresses
sess.rollback()
assert u.name == 'ed'
assert len(u.addresses) == 3
assert newad not in u.addresses
# pending objects don't get expired
assert newad.email_address == 'a new address'
示例10: test_deleted_flag
def test_deleted_flag(self):
users, User = self.tables.users, self.classes.User
mapper(User, users)
sess = sessionmaker()()
u1 = User(name='u1')
sess.add(u1)
sess.commit()
sess.delete(u1)
sess.flush()
assert u1 not in sess
assert_raises(sa.exc.InvalidRequestError, sess.add, u1)
sess.rollback()
assert u1 in sess
sess.delete(u1)
sess.commit()
assert u1 not in sess
assert_raises(sa.exc.InvalidRequestError, sess.add, u1)
make_transient(u1)
sess.add(u1)
sess.commit()
eq_(sess.query(User).count(), 1)
示例11: test_autoflush_expressions
def test_autoflush_expressions(self):
"""test that an expression which is dependent on object state is
evaluated after the session autoflushes. This is the lambda
inside of strategies.py lazy_clause.
"""
users, Address, addresses, User = (self.tables.users,
self.classes.Address,
self.tables.addresses,
self.classes.User)
mapper(User, users, properties={
'addresses': relationship(Address, backref="user")})
mapper(Address, addresses)
sess = create_session(autoflush=True, autocommit=False)
u = User(name='ed', addresses=[Address(email_address='foo')])
sess.add(u)
eq_(sess.query(Address).filter(Address.user == u).one(),
Address(email_address='foo'))
# still works after "u" is garbage collected
sess.commit()
sess.close()
u = sess.query(User).get(u.id)
q = sess.query(Address).filter(Address.user == u)
del u
gc_collect()
eq_(q.one(), Address(email_address='foo'))
示例12: setup_mappers
def setup_mappers(cls):
user, User = cls.tables.user, cls.classes.User
address, Address = cls.tables.address, cls.classes.Address
mapper(User, user, properties={
'addresses': relationship(Address, backref="user")
})
mapper(Address, address)
示例13: test_auto_detach_on_gc_session
def test_auto_detach_on_gc_session(self):
users, User = self.tables.users, self.classes.User
mapper(User, users)
sess = Session()
u1 = User(name='u1')
sess.add(u1)
sess.commit()
# can't add u1 to Session,
# already belongs to u2
s2 = Session()
assert_raises_message(
sa.exc.InvalidRequestError,
r".*is already attached to session",
s2.add, u1
)
# garbage collect sess
del sess
gc_collect()
# s2 lets it in now despite u1 having
# session_key
s2.add(u1)
assert u1 in s2
示例14: test_weakref_with_cycles_o2o
def test_weakref_with_cycles_o2o(self):
Address, addresses, users, User = (self.classes.Address,
self.tables.addresses,
self.tables.users,
self.classes.User)
s = sessionmaker()()
mapper(User, users, properties={
"address": relationship(Address, backref="user", uselist=False)
})
mapper(Address, addresses)
s.add(User(name="ed", address=Address(email_address="ed1")))
s.commit()
user = s.query(User).options(joinedload(User.address)).one()
user.address.user
eq_(user, User(name="ed", address=Address(email_address="ed1")))
del user
gc_collect()
assert len(s.identity_map) == 0
user = s.query(User).options(joinedload(User.address)).one()
user.address.email_address = 'ed2'
user.address.user # lazyload
del user
gc_collect()
assert len(s.identity_map) == 2
s.commit()
user = s.query(User).options(joinedload(User.address)).one()
eq_(user, User(name="ed", address=Address(email_address="ed2")))
示例15: setup_mappers
def setup_mappers(cls):
a, b = cls.tables.a, cls.tables.b
class A(cls.Comparable):
pass
class B(cls.Comparable):
pass
class C(cls.Comparable):
def __init__(self, b1, b2):
self.b1, self.b2 = b1, b2
def __composite_values__(self):
return self.b1, self.b2
def __eq__(self, other):
return isinstance(other, C) and \
other.b1 == self.b1 and \
other.b2 == self.b2
mapper(A, a, properties={
'b2':relationship(B),
'c':composite(C, 'b1', 'b2')
})
mapper(B, b)