Python sqlalchemy.orm.synonym() Examples
The following are 30
code examples of sqlalchemy.orm.synonym().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
sqlalchemy.orm
, or try the search function
.
Example #1
Source File: test_associationproxy.py From sqlalchemy with MIT License | 6 votes |
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")
Example #2
Source File: test_utils.py From sqlalchemy with MIT License | 6 votes |
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", )
Example #3
Source File: test_merge.py From sqlalchemy with MIT License | 6 votes |
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)
Example #4
Source File: test_mapper.py From sqlalchemy with MIT License | 6 votes |
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")
Example #5
Source File: test_mapper.py From sqlalchemy with MIT License | 6 votes |
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"})
Example #6
Source File: test_mapper.py From sqlalchemy with MIT License | 6 votes |
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
Example #7
Source File: test_update_delete.py From sqlalchemy with MIT License | 6 votes |
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")
Example #8
Source File: test_mapper.py From sqlalchemy with MIT License | 6 votes |
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, )
Example #9
Source File: test_mapper.py From sqlalchemy with MIT License | 5 votes |
def test_synonym_options(self): Address, addresses, users, User = ( self.classes.Address, self.tables.addresses, self.tables.users, self.classes.User, ) mapper( User, users, properties=dict( addresses=relationship( mapper(Address, addresses), lazy="select", order_by=addresses.c.id, ), adlist=synonym("addresses"), ), ) def go(): sess = create_session() u = ( sess.query(User) .order_by(User.id) .options(sa.orm.joinedload("adlist")) .filter_by(name="jack") ).one() eq_(u.adlist, [self.static.user_address_result[0].addresses[0]]) self.assert_sql_count(testing.db, go, 1)
Example #10
Source File: test_mapper.py From sqlalchemy with MIT License | 5 votes |
def test_synonym_of_synonym(self): users, User = (self.tables.users, self.classes.User) mapper(User, users, properties={"x": synonym("id"), "y": synonym("x")}) s = Session() u = s.query(User).filter(User.y == 8).one() eq_(u.y, 8)
Example #11
Source File: test_mapper.py From sqlalchemy with MIT License | 5 votes |
def test_synonym_get_history(self): users, User = (self.tables.users, self.classes.User) mapper(User, users, properties={"x": synonym("id"), "y": synonym("x")}) u1 = User() eq_(attributes.instance_state(u1).attrs.x.history, (None, None, None)) eq_(attributes.instance_state(u1).attrs.y.history, (None, None, None)) u1.y = 5 eq_(attributes.instance_state(u1).attrs.x.history, ([5], (), ())) eq_(attributes.instance_state(u1).attrs.y.history, ([5], (), ()))
Example #12
Source File: test_mapper.py From sqlalchemy with MIT License | 5 votes |
def test_synonym_map_column_conflict(self): users, User = self.tables.users, self.classes.User assert_raises( sa.exc.ArgumentError, mapper, User, users, properties=util.OrderedDict( [ ("_user_id", users.c.id), ("id", synonym("_user_id", map_column=True)), ] ), ) assert_raises( sa.exc.ArgumentError, mapper, User, users, properties=util.OrderedDict( [ ("id", synonym("_user_id", map_column=True)), ("_user_id", users.c.id), ] ), )
Example #13
Source File: test_mapper.py From sqlalchemy with MIT License | 5 votes |
def test_self_ref_synonym(self): t = Table( "nodes", MetaData(), Column( "id", Integer, primary_key=True, test_needs_autoincrement=True ), Column("parent_id", Integer, ForeignKey("nodes.id")), ) class Node(object): pass mapper( Node, t, properties={ "_children": relationship( Node, backref=backref("_parent", remote_side=t.c.id) ), "children": synonym("_children"), "parent": synonym("_parent"), }, ) n1 = Node() n2 = Node() n1.children.append(n2) assert n2.parent is n2._parent is n1 assert n1.children[0] is n1._children[0] is n2 eq_(str(Node.parent == n2), ":param_1 = nodes.parent_id")
Example #14
Source File: test_mapper.py From sqlalchemy with MIT License | 5 votes |
def test_synonym(self): users, User = self.tables.users, self.classes.User from sqlalchemy.orm.properties import ColumnProperty class MyFactory(ColumnProperty.Comparator): __hash__ = None def __eq__(self, other): return func.foobar(self.__clause_element__()) == func.foobar( other ) mapper( User, users, properties={ "name": synonym( "_name", map_column=True, comparator_factory=MyFactory ) }, ) self.assert_compile( User.name == "ed", "foobar(users.name) = foobar(:foobar_1)", dialect=default.DefaultDialect(), ) self.assert_compile( aliased(User).name == "ed", "foobar(users_1.name) = foobar(:foobar_1)", dialect=default.DefaultDialect(), )
Example #15
Source File: test_inspect.py From sqlalchemy with MIT License | 5 votes |
def setup_mappers(cls): cls._setup_stock_mapping() inspect(cls.classes.User).add_property("name_syn", synonym("name"))
Example #16
Source File: test_relationships.py From sqlalchemy with MIT License | 5 votes |
def test_synonym_fk(self): """test that active history is enabled on a one-to-many/one that has use_get==True""" tableB, A, B, tableA = ( self.tables.tableB, self.classes.A, self.classes.B, self.tables.tableA, ) mapper( B, tableB, properties={"a_id": synonym("_a_id", map_column=True)} ) mapper( A, tableA, properties={ "b": relationship( B, primaryjoin=(tableA.c.id == foreign(B.a_id)), uselist=False, ) }, ) sess = create_session() b = B(id=0) a = A(id=0, b=b) sess.add(a) sess.add(b) sess.flush() sess.expunge_all() assert a.b == b assert a.id == b.a_id assert a.id == b._a_id
Example #17
Source File: model_base.py From neutron-lib with Apache License 2.0 | 5 votes |
def tenant_id(cls): return orm.synonym( 'project_id', descriptor=property(cls.get_tenant_id, cls.set_tenant_id))
Example #18
Source File: dagrun.py From airflow with Apache License 2.0 | 5 votes |
def state(self): return synonym('_state', descriptor=property(self.get_state, self.set_state))
Example #19
Source File: test_mapper.py From sqlalchemy with MIT License | 5 votes |
def test_synonym_replaces_backref(self): addresses, users, User = ( self.tables.addresses, self.tables.users, self.classes.User, ) assert_calls = [] class Address(object): def _get_user(self): assert_calls.append("get") return self._user def _set_user(self, user): assert_calls.append("set") self._user = user user = property(_get_user, _set_user) # synonym is created against nonexistent prop mapper(Address, addresses, properties={"user": synonym("_user")}) sa.orm.configure_mappers() # later, backref sets up the prop mapper( User, users, properties={"addresses": relationship(Address, backref="_user")}, ) sess = create_session() u1 = sess.query(User).get(7) u2 = sess.query(User).get(8) # comparaison ops need to work a1 = sess.query(Address).filter(Address.user == u1).one() eq_(a1.id, 1) a1.user = u2 assert a1.user is u2 eq_(assert_calls, ["set", "get"])
Example #20
Source File: test_update_delete.py From sqlalchemy with MIT License | 5 votes |
def test_evaluate_synonym_attr(self): class Foo(object): pass mapper(Foo, self.tables.users, properties={"uname": synonym("name")}) s = Session() jill = s.query(Foo).get(3) s.query(Foo).update( {Foo.uname: "moonbeam"}, synchronize_session="evaluate" ) eq_(jill.uname, "moonbeam")
Example #21
Source File: test_joins.py From sqlalchemy with MIT License | 5 votes |
def setup_mappers(cls): User = cls.classes.User Address = cls.classes.Address users, addresses = (cls.tables.users, cls.tables.addresses) mapper( User, users, properties={ "addresses": relationship(Address), "ad_syn": synonym("addresses"), }, ) mapper(Address, addresses)
Example #22
Source File: test_basic.py From sqlalchemy with MIT License | 5 votes |
def test_polymorphic_synonym(self): class T1(fixtures.ComparableEntity): def info(self): return "THE INFO IS:" + self._info def _set_info(self, x): self._info = x info = property(info, _set_info) class T2(T1): pass mapper( T1, t1, polymorphic_on=t1.c.type, polymorphic_identity="t1", properties={"info": synonym("_info", map_column=True)}, ) mapper(T2, t2, inherits=T1, polymorphic_identity="t2") sess = create_session() at1 = T1(info="at1") at2 = T2(info="at2", data="t2 data") sess.add(at1) sess.add(at2) sess.flush() sess.expunge_all() eq_(sess.query(T2).filter(T2.info == "at2").one(), at2) eq_(at2.info, "THE INFO IS:at2")
Example #23
Source File: test_basic.py From sqlalchemy with MIT License | 5 votes |
def test_polymorphic_on_synonym(self): t1 = self.tables.t1 Parent = self.classes.Parent cprop = column_property(t1.c.x) assert_raises_message( sa_exc.ArgumentError, "Only direct column-mapped property or " "SQL expression can be passed for polymorphic_on", mapper, Parent, t1, properties={"discriminator": cprop, "discrim_syn": synonym(cprop)}, polymorphic_identity="parent", polymorphic_on="discrim_syn", )
Example #24
Source File: test_deprecations.py From sqlalchemy with MIT License | 5 votes |
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)
Example #25
Source File: test_deferred.py From sqlalchemy with MIT License | 5 votes |
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")
Example #26
Source File: answer.py From dokomoforms with GNU General Public License v3.0 | 5 votes |
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. """
Example #27
Source File: variable.py From airflow with Apache License 2.0 | 5 votes |
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))
Example #28
Source File: connection.py From airflow with Apache License 2.0 | 5 votes |
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))
Example #29
Source File: connection.py From airflow with Apache License 2.0 | 5 votes |
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))
Example #30
Source File: test_mapper.py From sqlalchemy with MIT License | 4 votes |
def test_add_property(self): users, addresses, Address = ( self.tables.users, self.tables.addresses, self.classes.Address, ) 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) mapper(Address, addresses) m.add_property("_name", deferred(users.c.name)) m.add_property("name", synonym("_name")) m.add_property("addresses", relationship(Address)) sess = create_session(autocommit=False) assert sess.query(User).get(7) u = sess.query(User).filter_by(name="jack").one() def go(): eq_( len(u.addresses), len(self.static.user_address_result[0].addresses), ) eq_(u.name, "jack") eq_(assert_col, [("get", "jack")], str(assert_col)) self.sql_count_(2, go) u.name = "ed" u3 = User() u3.name = "some user" sess.add(u3) sess.flush() sess.rollback()