本文整理汇总了Python中sqlalchemy.orm.synonym方法的典型用法代码示例。如果您正苦于以下问题:Python orm.synonym方法的具体用法?Python orm.synonym怎么用?Python orm.synonym使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.orm
的用法示例。
在下文中一共展示了orm.synonym方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_evaluate_double_synonym_attr
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import synonym [as 别名]
def test_evaluate_double_synonym_attr(self):
class Foo(object):
pass
mapper(
Foo,
self.tables.users,
properties={"uname": synonym("name"), "ufoo": synonym("uname")},
)
s = Session()
jill = s.query(Foo).get(3)
s.query(Foo).update(
{Foo.ufoo: "moonbeam"}, synchronize_session="evaluate"
)
eq_(jill.ufoo, "moonbeam")
示例2: test_column_prefix
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import synonym [as 别名]
def test_column_prefix(self):
users, User = self.tables.users, self.classes.User
mapper(
User,
users,
column_prefix="_",
properties={"user_name": synonym("_name")},
)
s = create_session()
u = s.query(User).get(7)
eq_(u._name, "jack")
eq_(u._id, 7)
u2 = s.query(User).filter_by(user_name="jack").one()
assert u is u2
示例3: test_info
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import synonym [as 别名]
def test_info(self):
users = self.tables.users
Address = self.classes.Address
class MyComposite(object):
pass
for constructor, args in [
(column_property, (users.c.name,)),
(relationship, (Address,)),
(composite, (MyComposite, "id", "name")),
(synonym, "foo"),
]:
obj = constructor(info={"x": "y"}, *args)
eq_(obj.info, {"x": "y"})
obj.info["q"] = "p"
eq_(obj.info, {"x": "y", "q": "p"})
obj = constructor(*args)
eq_(obj.info, {})
obj.info["q"] = "p"
eq_(obj.info, {"q": "p"})
示例4: test_synonym_nonexistent_attr
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import synonym [as 别名]
def test_synonym_nonexistent_attr(self):
# test [ticket:4767].
# synonym points to non-existent attrbute that hasn't been mapped yet.
users = self.tables.users
class User(object):
def _x(self):
return self.id
x = property(_x)
m = mapper(
User,
users,
properties={"x": synonym("some_attr", descriptor=User.x)},
)
# object gracefully handles this condition
assert not hasattr(User.x, "__name__")
assert not hasattr(User.x, "comparator")
m.add_property("some_attr", column_property(users.c.name))
assert not hasattr(User.x, "__name__")
assert hasattr(User.x, "comparator")
示例5: test_synonym_column_location
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import synonym [as 别名]
def test_synonym_column_location(self):
users, User = self.tables.users, self.classes.User
def go():
mapper(
User,
users,
properties={"not_name": synonym("_name", map_column=True)},
)
assert_raises_message(
sa.exc.ArgumentError,
(
"Can't compile synonym '_name': no column on table "
"'users' named 'not_name'"
),
go,
)
示例6: test_synonym
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import synonym [as 别名]
def test_synonym(self):
users = self.tables.users
class User(object):
def _getValue(self):
return self._value
def _setValue(self, value):
setattr(self, "_value", value)
value = property(_getValue, _setValue)
mapper(User, users, properties={"uid": synonym("id")})
sess = create_session()
u = User()
u.name = "ed"
sess.add(u)
sess.flush()
sess.expunge(u)
sess.merge(u)
示例7: test_proxy_descriptor_one
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import synonym [as 别名]
def test_proxy_descriptor_one(self):
class Point(object):
def __init__(self, x, y):
self.x, self.y = x, y
self._fixture(Point, properties={"x_syn": synonym("x")})
alias = aliased(Point)
eq_(str(Point.x_syn), "Point.x_syn")
eq_(str(alias.x_syn), "AliasedClass_Point.x_syn")
sess = Session()
self.assert_compile(
sess.query(alias.x_syn).filter(alias.x_syn > Point.x_syn),
"SELECT point_1.x AS point_1_x FROM point AS point_1, point "
"WHERE point_1.x > point.x",
)
示例8: setup_classes
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import synonym [as 别名]
def setup_classes(cls):
from sqlalchemy.orm import synonym
Base = cls.DeclarativeBasic
class A(Base):
__tablename__ = "a"
id = Column(Integer, primary_key=True)
data = Column(String)
bs = relationship("B", backref="a")
data_syn = synonym("data")
b_data = association_proxy("bs", "data_syn")
class B(Base):
__tablename__ = "b"
id = Column(Integer, primary_key=True)
a_id = Column(ForeignKey("a.id"))
data = Column(String)
data_syn = synonym("data")
a_data = association_proxy("a", "data_syn")
示例9: state
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import synonym [as 别名]
def state(self):
return synonym('_state', descriptor=property(self.get_state, self.set_state))
示例10: password
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import synonym [as 别名]
def password(cls): # pylint: disable=no-self-argument
"""Password. The value is decrypted/encrypted when reading/setting the value."""
return synonym('_password',
descriptor=property(cls.get_password, cls.set_password))
示例11: extra
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import synonym [as 别名]
def extra(cls): # pylint: disable=no-self-argument
"""Extra data. The value is decrypted/encrypted when reading/setting the value."""
return synonym('_extra',
descriptor=property(cls.get_extra, cls.set_extra))
示例12: val
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import synonym [as 别名]
def val(cls): # pylint: disable=no-self-argument
"""
Get Airflow Variable from Metadata DB and decode it using the Fernet Key
"""
return synonym('_val', descriptor=property(cls.get_val, cls.set_val))
示例13: answer
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import synonym [as 别名]
def answer(self):
"""The answer. Could be the same as main_answer in simple cases.
This property is the most useful representation available of the
answer. In the simplest case it is just a synonym for main_answer.
It could otherwise be a dictionary or another model.
"""
示例14: test_synonym_group_bug
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import synonym [as 别名]
def test_synonym_group_bug(self):
orders, Order = self.tables.orders, self.classes.Order
mapper(
Order,
orders,
properties={
"isopen": synonym("_isopen", map_column=True),
"description": deferred(orders.c.description, group="foo"),
},
)
sess = create_session()
o1 = sess.query(Order).get(1)
eq_(o1.description, "order 1")
示例15: test_add_property
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import synonym [as 别名]
def test_add_property(self):
users = self.tables.users
assert_col = []
class User(fixtures.ComparableEntity):
def _get_name(self):
assert_col.append(("get", self._name))
return self._name
def _set_name(self, name):
assert_col.append(("set", name))
self._name = name
name = property(_get_name, _set_name)
m = mapper(User, users)
m.add_property("_name", deferred(users.c.name))
m.add_property("name", synonym("_name"))
sess = create_session(autocommit=False)
assert sess.query(User).get(7)
u = sess.query(User).filter_by(name="jack").one()
def go():
eq_(u.name, "jack")
eq_(assert_col, [("get", "jack")], str(assert_col))
self.sql_count_(1, go)