本文整理汇总了Python中sqlalchemy.orm.create_session函数的典型用法代码示例。如果您正苦于以下问题:Python create_session函数的具体用法?Python create_session怎么用?Python create_session使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了create_session函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: test_o2m_flush_size
def test_o2m_flush_size(self):
mapper(User, users, properties={
'addresses':relationship(Address),
})
mapper(Address, addresses)
sess = create_session()
u1 = User(name='ed')
sess.add(u1)
self._assert_uow_size(sess, 2)
sess.flush()
u1.name='jack'
self._assert_uow_size(sess, 2)
sess.flush()
a1 = Address(email_address='foo')
sess.add(a1)
sess.flush()
u1.addresses.append(a1)
self._assert_uow_size(sess, 6)
sess.flush()
sess = create_session()
u1 = sess.query(User).first()
u1.name='ed'
self._assert_uow_size(sess, 2)
u1.addresses
self._assert_uow_size(sess, 6)
开发者ID:chatch,项目名称:pinyin-toolkit,代码行数:35,代码来源:test_unitofworkv2.py
示例2: test_singlecycle_flush_size
def test_singlecycle_flush_size(self):
Node, nodes = self.classes.Node, self.tables.nodes
mapper(Node, nodes, properties={
'children':relationship(Node)
})
sess = create_session()
n1 = Node(data='ed')
sess.add(n1)
self._assert_uow_size(sess, 2)
sess.flush()
n1.data='jack'
self._assert_uow_size(sess, 2)
sess.flush()
n2 = Node(data='foo')
sess.add(n2)
sess.flush()
n1.children.append(n2)
self._assert_uow_size(sess, 3)
sess.flush()
sess = create_session()
n1 = sess.query(Node).first()
n1.data='ed'
self._assert_uow_size(sess, 2)
n1.children
self._assert_uow_size(sess, 2)
开发者ID:ContextLogic,项目名称:sqlalchemy,代码行数:35,代码来源:test_unitofworkv2.py
示例3: test_versioncheck
def test_versioncheck(self):
"""query.with_lockmode performs a 'version check' on an already loaded instance"""
s1 = create_session(autocommit=False)
mapper(Foo, version_table, version_id_col=version_table.c.version_id)
f1s1 = Foo(value='f1 value')
s1.add(f1s1)
s1.commit()
s2 = create_session(autocommit=False)
f1s2 = s2.query(Foo).get(f1s1.id)
f1s2.value='f1 new value'
s2.commit()
# load, version is wrong
assert_raises_message(
sa.orm.exc.StaleDataError,
r"Instance .* has version id '\d+' which does not "
r"match database-loaded version id '\d+'",
s1.query(Foo).with_lockmode('read').get, f1s1.id
)
# reload it - this expires the old version first
s1.refresh(f1s1, lockmode='read')
# now assert version OK
s1.query(Foo).with_lockmode('read').get(f1s1.id)
# assert brand new load is OK too
s1.close()
s1.query(Foo).with_lockmode('read').get(f1s1.id)
开发者ID:chatch,项目名称:pinyin-toolkit,代码行数:32,代码来源:test_versioning.py
示例4: test_class_deferred_cols
def test_class_deferred_cols(self):
addresses, users = (self.tables.addresses,
self.tables.users)
mapper(User, users, properties={
'name': sa.orm.deferred(users.c.name),
'addresses': relationship(Address, backref="user")
})
mapper(Address, addresses, properties={
'email_address': sa.orm.deferred(addresses.c.email_address)
})
sess = create_session()
u1 = User(name='ed')
u1.addresses.append(Address(email_address='[email protected]'))
sess.add(u1)
sess.flush()
sess.expunge_all()
u1 = sess.query(User).get(u1.id)
assert 'name' not in u1.__dict__
assert 'addresses' not in u1.__dict__
u2 = pickle.loads(pickle.dumps(u1))
sess2 = create_session()
sess2.add(u2)
eq_(u2.name, 'ed')
eq_(u2, User(name='ed', addresses=[Address(email_address='[email protected]')]))
u2 = pickle.loads(pickle.dumps(u1))
sess2 = create_session()
u2 = sess2.merge(u2, load=False)
eq_(u2.name, 'ed')
eq_(u2, User(name='ed', addresses=[Address(email_address='[email protected]')]))
开发者ID:afeide,项目名称:LuoYunCloud,代码行数:32,代码来源:test_pickled.py
示例5: test_no_load_sets_backrefs
def test_no_load_sets_backrefs(self):
users, Address, addresses, User = (self.tables.users,
self.classes.Address,
self.tables.addresses,
self.classes.User)
mapper(User, users, properties={
'addresses':relationship(mapper(Address, addresses),
backref='user')})
sess = create_session()
u = User()
u.id = 7
u.name = "fred"
a1 = Address()
a1.email_address='[email protected]'
u.addresses.append(a1)
sess.add(u)
sess.flush()
assert u.addresses[0].user is u
sess2 = create_session()
u2 = sess2.merge(u, load=False)
assert not sess2.dirty
def go():
assert u2.addresses[0].user is u2
self.assert_sql_count(testing.db, go, 0)
开发者ID:23andMe,项目名称:sqlalchemy,代码行数:29,代码来源:test_merge.py
示例6: test_identity_conflict
def test_identity_conflict(self):
users, User = self.tables.users, self.classes.User
mapper(User, users)
for s in (
create_session(),
create_session(weak_identity_map=False),
):
users.delete().execute()
u1 = User(name="ed")
s.add(u1)
s.flush()
s.expunge(u1)
u2 = s.query(User).first()
s.expunge(u2)
s.identity_map.add(sa.orm.attributes.instance_state(u1))
assert_raises_message(
sa.exc.InvalidRequestError,
"Can't attach instance <User.*?>; another instance "
"with key .*? is already "
"present in this session.",
s.identity_map.add,
sa.orm.attributes.instance_state(u2)
)
开发者ID:CyberCollins,项目名称:sqlalchemy,代码行数:25,代码来源:test_session.py
示例7: test_no_load_with_backrefs
def test_no_load_with_backrefs(self):
"""load=False populates relationships in both directions without requiring a load"""
mapper(User, users, properties={
'addresses':relationship(mapper(Address, addresses), backref='user')
})
u = User(id=7, name='fred', addresses=[
Address(email_address='ad1'),
Address(email_address='ad2')])
sess = create_session()
sess.add(u)
sess.flush()
sess.close()
assert 'user' in u.addresses[1].__dict__
sess = create_session()
u2 = sess.merge(u, load=False)
assert 'user' in u2.addresses[1].__dict__
eq_(u2.addresses[1].user, User(id=7, name='fred'))
sess.expire(u2.addresses[1], ['user'])
assert 'user' not in u2.addresses[1].__dict__
sess.close()
sess = create_session()
u = sess.merge(u2, load=False)
assert 'user' not in u.addresses[1].__dict__
eq_(u.addresses[1].user, User(id=7, name='fred'))
开发者ID:chatch,项目名称:pinyin-toolkit,代码行数:28,代码来源:test_merge.py
示例8: test_add_delete
def test_add_delete(self):
User, Address, addresses, users = (self.classes.User,
self.classes.Address,
self.tables.addresses,
self.tables.users)
s = create_session()
mapper(User, users, properties={
'addresses': relationship(Address, cascade="all, delete")
})
mapper(Address, addresses)
user = User(name='u1')
assert_raises_message(sa.exc.InvalidRequestError,
'is not persisted', s.delete, user)
s.add(user)
s.flush()
user = s.query(User).one()
s.expunge(user)
assert user not in s
# modify outside of session, assert changes remain/get saved
user.name = "fred"
s.add(user)
assert user in s
assert user in s.dirty
s.flush()
s.expunge_all()
assert s.query(User).count() == 1
user = s.query(User).one()
assert user.name == 'fred'
# ensure its not dirty if no changes occur
s.expunge_all()
assert user not in s
s.add(user)
assert user in s
assert user not in s.dirty
s2 = create_session()
assert_raises_message(sa.exc.InvalidRequestError,
'is already attached to session',
s2.delete, user)
u2 = s2.query(User).get(user.id)
s2.expunge(u2)
assert_raises_message(
sa.exc.InvalidRequestError,
'another instance .* is already present', s.delete, u2)
s.expire(user)
s.expunge(user)
assert user not in s
s.delete(user)
assert user in s
s.flush()
assert user not in s
assert s.query(User).count() == 0
开发者ID:CyberCollins,项目名称:sqlalchemy,代码行数:60,代码来源:test_session.py
示例9: test_versioncheck
def test_versioncheck(self):
"""query.with_lockmode performs a 'version check' on an already loaded instance"""
s1 = create_session(autocommit=False)
mapper(Foo, version_table, version_id_col=version_table.c.version_id)
f1s1 = Foo(value='f1 value')
s1.add(f1s1)
s1.commit()
s2 = create_session(autocommit=False)
f1s2 = s2.query(Foo).get(f1s1.id)
f1s2.value='f1 new value'
s2.commit()
# load, version is wrong
assert_raises(
sa.orm.exc.ConcurrentModificationError,
s1.query(Foo).with_lockmode('read').get, f1s1.id
)
# reload it - this expires the old version first
s1.refresh(f1s1, lockmode='read')
# now assert version OK
s1.query(Foo).with_lockmode('read').get(f1s1.id)
# assert brand new load is OK too
s1.close()
s1.query(Foo).with_lockmode('read').get(f1s1.id)
开发者ID:gaguilarmi,项目名称:sqlalchemy,代码行数:30,代码来源:test_versioning.py
示例10: test_many_to_one_cascade
def test_many_to_one_cascade(self):
mapper(Address, addresses, properties={
'user':relationship(User)
})
mapper(User, users)
u1 = User(id=1, name="u1")
a1 =Address(id=1, email_address="a1", user=u1)
u2 = User(id=2, name="u2")
sess = create_session()
sess.add_all([a1, u2])
sess.flush()
a1.user = u2
sess2 = create_session()
a2 = sess2.merge(a1)
eq_(
attributes.get_history(a2, 'user'),
([u2], (), [attributes.PASSIVE_NO_RESULT])
)
assert a2 in sess2.dirty
sess.refresh(a1)
sess2 = create_session()
a2 = sess2.merge(a1, load=False)
eq_(
attributes.get_history(a2, 'user'),
((), [u1], ())
)
assert a2 not in sess2.dirty
开发者ID:chatch,项目名称:pinyin-toolkit,代码行数:33,代码来源:test_merge.py
示例11: test_one_to_one_cascade
def test_one_to_one_cascade(self):
users, Address, addresses, User = (
self.tables.users,
self.classes.Address,
self.tables.addresses,
self.classes.User,
)
mapper(User, users, properties={"address": relationship(mapper(Address, addresses), uselist=False)})
load = self.load_tracker(User)
self.load_tracker(Address, load)
sess = create_session()
u = User()
u.id = 7
u.name = "fred"
a1 = Address()
a1.email_address = "[email protected]"
u.address = a1
sess.add(u)
sess.flush()
eq_(load.called, 0)
sess2 = create_session()
u2 = sess2.query(User).get(7)
eq_(load.called, 1)
u2.name = "fred2"
u2.address.email_address = "[email protected]"
eq_(load.called, 2)
u3 = sess.merge(u2)
eq_(load.called, 2)
assert u3 is u
开发者ID:pugong,项目名称:sqlalchemy,代码行数:35,代码来源:test_merge.py
示例12: test_no_load_with_backrefs
def test_no_load_with_backrefs(self):
"""load=False populates relationships in both
directions without requiring a load"""
users, Address, addresses, User = (
self.tables.users,
self.classes.Address,
self.tables.addresses,
self.classes.User,
)
mapper(User, users, properties={"addresses": relationship(mapper(Address, addresses), backref="user")})
u = User(id=7, name="fred", addresses=[Address(email_address="ad1"), Address(email_address="ad2")])
sess = create_session()
sess.add(u)
sess.flush()
sess.close()
assert "user" in u.addresses[1].__dict__
sess = create_session()
u2 = sess.merge(u, load=False)
assert "user" in u2.addresses[1].__dict__
eq_(u2.addresses[1].user, User(id=7, name="fred"))
sess.expire(u2.addresses[1], ["user"])
assert "user" not in u2.addresses[1].__dict__
sess.close()
sess = create_session()
u = sess.merge(u2, load=False)
assert "user" not in u.addresses[1].__dict__
eq_(u.addresses[1].user, User(id=7, name="fred"))
开发者ID:pugong,项目名称:sqlalchemy,代码行数:33,代码来源:test_merge.py
示例13: test_deferred_cols_missing_in_load_state_reset
def test_deferred_cols_missing_in_load_state_reset(self):
Data = self.classes.DataDefer
sess = create_session()
d1 = Data(data='d1')
sess.add(d1)
sess.flush()
sess.close()
sess = create_session()
d1 = sess.query(Data).from_statement(
select([Data.id])).options(undefer(Data.data)).first()
d1.data = 'd2'
# the deferred loader has to clear out any state
# on the col, including that 'd2' here
d1 = sess.query(Data).populate_existing().first()
def go():
eq_(d1.data, 'd1')
self.assert_sql_count(
testing.db, go, 1
)
开发者ID:Attsun1031,项目名称:sqlalchemy,代码行数:25,代码来源:test_expire.py
示例14: test_joinedload_on_subclass
def test_joinedload_on_subclass(self):
sess = create_session()
expected = [
Engineer(
name="dilbert",
engineer_name="dilbert",
primary_language="java",
status="regular engineer",
machines=[
Machine(name="IBM ThinkPad"),
Machine(name="IPhone")])]
def go():
# test load People with joinedload to engineers + machines
eq_(sess.query(Person)
.with_polymorphic('*')
.options(joinedload(Engineer.machines))
.filter(Person.name == 'dilbert').all(),
expected)
self.assert_sql_count(testing.db, go, 1)
sess = create_session()
def go():
# test load People with subqueryload to engineers + machines
eq_(sess.query(Person)
.with_polymorphic('*')
.options(subqueryload(Engineer.machines))
.filter(Person.name == 'dilbert').all(),
expected)
self.assert_sql_count(testing.db, go, 2)
开发者ID:afeide,项目名称:LuoYunCloud,代码行数:30,代码来源:test_polymorphic_rel.py
示例15: test_relationship_to_polymorphic_three
def test_relationship_to_polymorphic_three(self):
expected = self._company_with_emps_machines_fixture()
sess = create_session()
sess = create_session()
def go():
eq_(sess.query(Company)
.options(subqueryload_all(
Company.employees.of_type(Engineer),
Engineer.machines))
.all(),
expected)
# the old case where subqueryload_all
# didn't work with of_tyoe
#count = { '':8, 'Joins':4, 'Unions':4, 'Polymorphic':3,
# 'AliasedJoins':4}[self.select_type]
# query one is company->Person/Engineer->Machines
# query two is Person/Engineer subq
# query three is Machines subq
# (however this test can't tell if the Q was a
# lazyload or subqload ...)
# query four is managers + boss for row #3
# query five is managers for row #4
count = 5
self.assert_sql_count(testing.db, go, count)
开发者ID:afeide,项目名称:LuoYunCloud,代码行数:27,代码来源:test_polymorphic_rel.py
示例16: test_dontload_with_eager
def test_dontload_with_eager(self):
"""
This test illustrates that with load=False, we can't just copy the
committed_state of the merged instance over; since it references
collection objects which themselves are to be merged. This
committed_state would instead need to be piecemeal 'converted' to
represent the correct objects. However, at the moment I'd rather not
support this use case; if you are merging with load=False, you're
typically dealing with caching and the merged objects shouldnt be
'dirty'.
"""
mapper(User, users, properties={
'addresses':relationship(mapper(Address, addresses))
})
sess = create_session()
u = User()
u.id = 7
u.name = "fred"
a1 = Address()
a1.email_address='[email protected]'
u.addresses.append(a1)
sess.add(u)
sess.flush()
sess2 = create_session()
u2 = sess2.query(User).options(sa.orm.joinedload('addresses')).get(7)
sess3 = create_session()
u3 = sess3.merge(u2, load=False)
def go():
sess3.flush()
self.assert_sql_count(testing.db, go, 0)
开发者ID:chatch,项目名称:pinyin-toolkit,代码行数:35,代码来源:test_merge.py
示例17: test_bound_connection_transactional
def test_bound_connection_transactional(self):
User, users = self.classes.User, self.tables.users
mapper(User, users)
c = testing.db.connect()
sess = create_session(bind=c, autocommit=False)
u = User(name='u1')
sess.add(u)
sess.flush()
sess.close()
assert not c.in_transaction()
assert c.scalar("select count(1) from users") == 0
sess = create_session(bind=c, autocommit=False)
u = User(name='u2')
sess.add(u)
sess.flush()
sess.commit()
assert not c.in_transaction()
assert c.scalar("select count(1) from users") == 1
c.execute("delete from users")
assert c.scalar("select count(1) from users") == 0
c = testing.db.connect()
trans = c.begin()
sess = create_session(bind=c, autocommit=True)
u = User(name='u3')
sess.add(u)
sess.flush()
assert c.in_transaction()
trans.commit()
assert not c.in_transaction()
assert c.scalar("select count(1) from users") == 1
开发者ID:Attsun1031,项目名称:sqlalchemy,代码行数:35,代码来源:test_bind.py
示例18: test_no_load_disallows_dirty
def test_no_load_disallows_dirty(self):
"""load=False doesnt support 'dirty' objects right now
(see test_no_load_with_eager()). Therefore lets assert it.
"""
users, User = self.tables.users, self.classes.User
mapper(User, users)
sess = create_session()
u = User()
u.id = 7
u.name = "fred"
sess.add(u)
sess.flush()
u.name = 'ed'
sess2 = create_session()
try:
sess2.merge(u, load=False)
assert False
except sa.exc.InvalidRequestError, e:
assert ("merge() with load=False option does not support "
"objects marked as 'dirty'. flush() all changes on mapped "
"instances before merging with load=False.") in str(e)
开发者ID:onetera,项目名称:scandatatransfer,代码行数:26,代码来源:test_merge.py
示例19: test_limit
def test_limit(self):
"""Limit operations combined with lazy-load relationships."""
users, items, order_items, orders, Item, User, Address, Order, addresses = (self.tables.users,
self.tables.items,
self.tables.order_items,
self.tables.orders,
self.classes.Item,
self.classes.User,
self.classes.Address,
self.classes.Order,
self.tables.addresses)
mapper(Item, items)
mapper(Order, orders, properties={
'items':relationship(Item, secondary=order_items, lazy='subquery',
order_by=items.c.id)
})
mapper(User, users, properties={
'addresses':relationship(mapper(Address, addresses),
lazy='subquery',
order_by=addresses.c.id),
'orders':relationship(Order, lazy='select', order_by=orders.c.id)
})
sess = create_session()
q = sess.query(User)
l = q.order_by(User.id).limit(2).offset(1).all()
eq_(self.static.user_all_result[1:3], l)
sess = create_session()
l = q.order_by(sa.desc(User.id)).limit(2).offset(2).all()
eq_(list(reversed(self.static.user_all_result[0:2])), l)
开发者ID:sleepsonthefloor,项目名称:sqlalchemy,代码行数:35,代码来源:test_subquery_relations.py
示例20: test_one_to_one_cascade
def test_one_to_one_cascade(self):
mapper(User, users, properties={
'address':relationship(mapper(Address, addresses),uselist = False)
})
on_load = self.on_load_tracker(User)
self.on_load_tracker(Address, on_load)
sess = create_session()
u = User()
u.id = 7
u.name = "fred"
a1 = Address()
a1.email_address='[email protected]'
u.address = a1
sess.add(u)
sess.flush()
eq_(on_load.called, 0)
sess2 = create_session()
u2 = sess2.query(User).get(7)
eq_(on_load.called, 1)
u2.name = 'fred2'
u2.address.email_address = '[email protected]'
eq_(on_load.called, 2)
u3 = sess.merge(u2)
eq_(on_load.called, 2)
assert u3 is u
开发者ID:chatch,项目名称:pinyin-toolkit,代码行数:31,代码来源:test_merge.py
注:本文中的sqlalchemy.orm.create_session函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论