Python sqlalchemy.ext.associationproxy.association_proxy() Examples
The following are 30
code examples of sqlalchemy.ext.associationproxy.association_proxy().
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.ext.associationproxy
, or try the search function
.
Example #1
Source File: test_associationproxy.py From sqlalchemy with MIT License | 6 votes |
def test_resolved_to_correct_class_two(self): Base = declarative_base() class Parent(Base): __tablename__ = "parent" id = Column(Integer, primary_key=True) _children = relationship("Child") class Child(Base): __tablename__ = "child" parent_id = Column( Integer, ForeignKey(Parent.id), primary_key=True ) value = Column(String) class SubParent(Parent): __tablename__ = "subparent" id = Column(Integer, ForeignKey(Parent.id), primary_key=True) children = association_proxy("_children", "value") is_(SubParent.children.owning_class, SubParent)
Example #2
Source File: __init__.py From pyA2L with GNU General Public License v2.0 | 6 votes |
def annotation_association(cls): name = cls.__name__ discriminator = name.lower() assoc_cls = type( "%sAnnotationAssociation" % name, (AnnotationAssociation,), dict( __tablename__ = None, __mapper_args__ = {"polymorphic_identity": discriminator}, ), ) cls.annotation = association_proxy( "annotation_association", "annotation", creator = lambda annotation: assoc_cls(annotation = annotation), ) return relationship( assoc_cls, backref = backref("parent", uselist = False, collection_class = ordering_list('position')) )
Example #3
Source File: __init__.py From pyA2L with GNU General Public License v2.0 | 6 votes |
def if_data_association(cls): name = cls.__name__ discriminator = name.lower() assoc_cls = type( "%sIfDataAssociation" % name, (IfDataAssociation,), dict( __tablename__ = None, __mapper_args__ = {"polymorphic_identity": discriminator}, ), ) cls.if_data = association_proxy( "if_data_association", "if_data", creator = lambda if_data: assoc_cls(if_data = if_data), ) return relationship( assoc_cls, backref = backref("parent", uselist = False, collection_class = ordering_list('position')) )
Example #4
Source File: test_associationproxy.py From sqlalchemy with MIT License | 6 votes |
def test_resolved_to_correct_class_one(self): Base = declarative_base() class Parent(Base): __tablename__ = "parent" id = Column(Integer, primary_key=True) _children = relationship("Child") children = association_proxy("_children", "value") class Child(Base): __tablename__ = "child" parent_id = Column( Integer, ForeignKey(Parent.id), primary_key=True ) value = Column(String) class SubParent(Parent): __tablename__ = "subparent" id = Column(Integer, ForeignKey(Parent.id), primary_key=True) is_(SubParent.children.owning_class, SubParent) is_(Parent.children.owning_class, Parent)
Example #5
Source File: discriminator_on_association.py From sqlalchemy with MIT License | 6 votes |
def address_association(cls): name = cls.__name__ discriminator = name.lower() assoc_cls = type( "%sAddressAssociation" % name, (AddressAssociation,), dict( __tablename__=None, __mapper_args__={"polymorphic_identity": discriminator}, ), ) cls.addresses = association_proxy( "address_association", "addresses", creator=lambda addresses: assoc_cls(addresses=addresses), ) return relationship( assoc_cls, backref=backref("parent", uselist=False) )
Example #6
Source File: test_associationproxy.py From sqlalchemy with MIT License | 6 votes |
def setup_classes(cls): Base = cls.DeclarativeBasic class A(Base): __tablename__ = "a" id = Column(Integer, primary_key=True) data = Column(String(50)) bs = relationship("B") b_dyn = relationship("B", lazy="dynamic", viewonly=True) b_data = association_proxy("bs", "data") b_dynamic_data = association_proxy("bs", "data") class B(Base): __tablename__ = "b" id = Column(Integer, primary_key=True) aid = Column(ForeignKey("a.id")) data = Column(String(50))
Example #7
Source File: test_associationproxy.py From sqlalchemy with MIT License | 6 votes |
def test_resolve_aliased_class(self): Base = declarative_base() class A(Base): __tablename__ = "a" id = Column(Integer, primary_key=True) value = Column(String) class B(Base): __tablename__ = "b" id = Column(Integer, primary_key=True) a_id = Column(Integer, ForeignKey(A.id)) a = relationship(A) a_value = association_proxy("a", "value") spec = aliased(B).a_value is_(spec.owning_class, B) spec = B.a_value is_(spec.owning_class, B)
Example #8
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 #9
Source File: test_associationproxy.py From sqlalchemy with MIT License | 6 votes |
def setup(self): metadata = MetaData(testing.db) parents = Table( "parents", metadata, Column( "id", Integer, primary_key=True, test_needs_autoincrement=True ), Column("name", String(30)), ) children = Table( "children", metadata, Column( "id", Integer, primary_key=True, test_needs_autoincrement=True ), Column("parent_id", Integer, ForeignKey("parents.id")), Column("name", String(30)), ) metadata.create_all() parents.insert().execute(name="p1") self.metadata = metadata self.parents = parents self.children = children Parent.kids = association_proxy("children", "name")
Example #10
Source File: test_associationproxy.py From sqlalchemy with MIT License | 6 votes |
def test_lazy_list(self): Parent, Child = self.Parent, self.Child mapper( Parent, self.table, properties={ "_children": relationship( Child, lazy="select", collection_class=list ) }, ) p = Parent("p") p.children = ["a", "b", "c"] p = self.roundtrip(p) # Is there a better way to ensure that the association_proxy # didn't convert a lazy load to an eager load? This does work though. self.assert_("_children" not in p.__dict__) self.assert_(len(p._children) == 3) self.assert_("_children" in p.__dict__)
Example #11
Source File: test_associationproxy.py From sqlalchemy with MIT License | 5 votes |
def setup_classes(cls): Base = cls.DeclarativeBasic class Foo(Base): __tablename__ = "foo" id = Column(Integer, primary_key=True) foo = Column(String) # assume some composite datatype bar = association_proxy("foo", "attr")
Example #12
Source File: test_associationproxy.py From sqlalchemy with MIT License | 5 votes |
def test_resolved_to_correct_class_five(self): Base = declarative_base() class Mixin(object): children = association_proxy("_children", "value") class Parent(Mixin, Base): __tablename__ = "parent" id = Column(Integer, primary_key=True) _children = relationship("Child") class Child(Base): __tablename__ = "child" parent_id = Column( Integer, ForeignKey(Parent.id), primary_key=True ) value = Column(String) # this triggers the owning routine, doesn't fail Mixin.children p1 = Parent() c1 = Child(value="c1") p1._children.append(c1) is_(Parent.children.owning_class, Parent) eq_(p1.children, ["c1"])
Example #13
Source File: test_associationproxy.py From sqlalchemy with MIT License | 5 votes |
def _test_never_assign_nonetype(self): foo = association_proxy("x", "y") foo._calc_owner(None, None) is_(foo.owning_class, None) class Bat(object): foo = association_proxy("x", "y") Bat.foo is_(Bat.foo.owning_class, None) b1 = Bat() assert_raises_message( exc.InvalidRequestError, "This association proxy has no mapped owning class; " "can't locate a mapped property", getattr, b1, "foo", ) is_(Bat.foo.owning_class, None) # after all that, we can map it mapper( Bat, Table("bat", MetaData(), Column("x", Integer, primary_key=True)), ) # answer is correct is_(Bat.foo.owning_class, Bat)
Example #14
Source File: test_associationproxy.py From sqlalchemy with MIT License | 5 votes |
def setup_classes(cls): Base = cls.DeclarativeBasic class A(Base): __tablename__ = "test_a" id = Column(Integer, primary_key=True) ab = relationship("AB", backref="a", uselist=cls.uselist) b = association_proxy( "ab", "b", creator=lambda b: AB(b=b), cascade_scalar_deletes=cls.cascade_scalar_deletes, ) if cls.useobject: class B(Base): __tablename__ = "test_b" id = Column(Integer, primary_key=True) ab = relationship("AB", backref="b") class AB(Base): __tablename__ = "test_ab" a_id = Column(Integer, ForeignKey(A.id), primary_key=True) b_id = Column(Integer, ForeignKey(B.id), primary_key=True) else: class AB(Base): __tablename__ = "test_ab" b = Column(Integer) a_id = Column(Integer, ForeignKey(A.id), primary_key=True)
Example #15
Source File: test_associationproxy.py From sqlalchemy with MIT License | 5 votes |
def test_constructor(self): assoc = association_proxy("a", "b", info={"some_assoc": "some_value"}) eq_(assoc.info, {"some_assoc": "some_value"})
Example #16
Source File: test_associationproxy.py From sqlalchemy with MIT License | 5 votes |
def test_via_cls(self): class Foob(object): assoc = association_proxy("a", "b") eq_(Foob.assoc.info, {}) Foob.assoc.info["foo"] = "bar" eq_(Foob.assoc.info, {"foo": "bar"})
Example #17
Source File: standard_attr.py From neutron-lib with Apache License 2.0 | 5 votes |
def revision_number(cls): return association_proxy('standard_attr', 'revision_number')
Example #18
Source File: test_associationproxy.py From sqlalchemy with MIT License | 5 votes |
def setup_classes(cls): Base = cls.DeclarativeBasic class A(Base): __tablename__ = "a" id = Column(Integer, primary_key=True) bs = relationship("B") b_data = association_proxy("bs", "value") class B(Base): __tablename__ = "b" id = Column(Integer, primary_key=True) aid = Column(ForeignKey("a.id")) data = Column(String(50)) @property def value(self): return self.data @value.setter def value(self, value): self.data = value
Example #19
Source File: standard_attr.py From neutron-lib with Apache License 2.0 | 5 votes |
def description(cls): return association_proxy('standard_attr', 'description')
Example #20
Source File: standard_attr.py From neutron-lib with Apache License 2.0 | 5 votes |
def created_at(cls): return association_proxy('standard_attr', 'created_at')
Example #21
Source File: standard_attr.py From neutron-lib with Apache License 2.0 | 5 votes |
def updated_at(cls): return association_proxy('standard_attr', 'updated_at')
Example #22
Source File: transaction_meta.py From ReadableWebProxy with BSD 3-Clause "New" or "Revised" License | 5 votes |
def create_class(self, manager): """ Create TransactionMeta class. """ class TransactionMeta( manager.declarative_base, TransactionMetaBase ): __tablename__ = 'transaction_meta' TransactionMeta.transaction = sa.orm.relationship( manager.transaction_cls, backref=sa.orm.backref( 'meta_relation', collection_class=attribute_mapped_collection('key') ), primaryjoin=( '%s.id == TransactionMeta.transaction_id' % manager.transaction_cls.__name__ ), foreign_keys=[TransactionMeta.transaction_id] ) manager.transaction_cls.meta = association_proxy( 'meta_relation', 'value', creator=lambda key, value: TransactionMeta(key=key, value=value) ) return TransactionMeta
Example #23
Source File: test_associationproxy.py From sqlalchemy with MIT License | 5 votes |
def test_resolved_to_correct_class_four(self): Base = declarative_base() class Parent(Base): __tablename__ = "parent" id = Column(Integer, primary_key=True) _children = relationship("Child") children = association_proxy( "_children", "value", creator=lambda value: Child(value=value) ) class Child(Base): __tablename__ = "child" parent_id = Column( Integer, ForeignKey(Parent.id), primary_key=True ) value = Column(String) class SubParent(Parent): __tablename__ = "subparent" id = Column(Integer, ForeignKey(Parent.id), primary_key=True) sp = SubParent() sp.children = "c" is_(SubParent.children.owning_class, SubParent) is_(Parent.children.owning_class, Parent)
Example #24
Source File: test_associationproxy.py From sqlalchemy with MIT License | 5 votes |
def test_resolved_w_subclass(self): # test for issue #4185, as well as several below Base = declarative_base() class Mixin(object): @declared_attr def children(cls): return association_proxy("_children", "value") # 1. build parent, Mixin.children gets invoked, we add # Parent.children class Parent(Mixin, Base): __tablename__ = "parent" id = Column(Integer, primary_key=True) _children = relationship("Child") class Child(Base): __tablename__ = "child" parent_id = Column( Integer, ForeignKey(Parent.id), primary_key=True ) value = Column(String) # 2. declarative builds up SubParent, scans through all attributes # over all classes. Hits Mixin, hits "children", accesses "children" # in terms of the class, e.g. SubParent.children. SubParent isn't # mapped yet. association proxy then sets up "owning_class" # as NoneType. class SubParent(Parent): __tablename__ = "subparent" id = Column(Integer, ForeignKey(Parent.id), primary_key=True) configure_mappers() # 3. which would break here. p1 = Parent() eq_(p1.children, [])
Example #25
Source File: test_associationproxy.py From sqlalchemy with MIT License | 5 votes |
def setup(self): metadata = MetaData(testing.db) parents_table = Table( "Parent", metadata, Column( "id", Integer, primary_key=True, test_needs_autoincrement=True ), Column("name", String(128)), ) children_table = Table( "Children", metadata, Column( "id", Integer, primary_key=True, test_needs_autoincrement=True ), Column("parent_id", Integer, ForeignKey("Parent.id")), Column("foo", String(128)), Column("name", String(128)), ) class Parent(object): children = association_proxy("_children", "name") def __init__(self, name): self.name = name class Child(object): def __init__(self, name): self.name = name mapper(Child, children_table) metadata.create_all() self.metadata = metadata self.session = create_session() self.Parent, self.Child = Parent, Child self.table = parents_table
Example #26
Source File: test_associationproxy.py From sqlalchemy with MIT License | 5 votes |
def test_custom_getset(self): metadata = MetaData() p = Table( "p", metadata, Column("id", Integer, primary_key=True), Column("cid", Integer, ForeignKey("c.id")), ) c = Table( "c", metadata, Column("id", Integer, primary_key=True), Column("foo", String(128)), ) get = Mock() set_ = Mock() class Parent(object): foo = association_proxy( "child", "foo", getset_factory=lambda cc, parent: (get, set_) ) class Child(object): def __init__(self, foo): self.foo = foo mapper(Parent, p, properties={"child": relationship(Child)}) mapper(Child, c) p1 = Parent() eq_(p1.foo, get(None)) p1.child = child = Child(foo="x") eq_(p1.foo, get(child)) p1.foo = "y" eq_(set_.mock_calls, [call(child, "y")])
Example #27
Source File: test_associationproxy.py From sqlalchemy with MIT License | 5 votes |
def _fixture(self, collection_class, is_dict=False): class Parent(object): collection = association_proxy("_collection", "child") class Child(object): def __init__(self, name): self.name = name class Association(object): if is_dict: def __init__(self, key, child): self.child = child else: def __init__(self, child): self.child = child mapper( Parent, self.tables.parent, properties={ "_collection": relationship( Association, collection_class=collection_class, backref="parent", ) }, ) mapper( Association, self.tables.association, properties={"child": relationship(Child, backref="association")}, ) mapper(Child, self.tables.child) return Parent, Child, Association
Example #28
Source File: test_mapper.py From sqlalchemy with MIT License | 5 votes |
def test_synonym_of_non_property_raises(self): from sqlalchemy.ext.associationproxy import association_proxy class User(object): pass users, Address, addresses = ( self.tables.users, self.classes.Address, self.tables.addresses, ) mapper( User, users, properties={"y": synonym("x"), "addresses": relationship(Address)}, ) mapper(Address, addresses) User.x = association_proxy("addresses", "email_address") assert_raises_message( sa.exc.InvalidRequestError, r'synonym\(\) attribute "User.x" only supports ORM mapped ' "attributes, got .*AssociationProxy", getattr, User.y, "property", )
Example #29
Source File: relationship.py From incubator-ariatosca with Apache License 2.0 | 5 votes |
def association_proxy(*args, **kwargs): if 'type' in kwargs: type_ = kwargs.get('type') del kwargs['type'] else: type_ = ':obj:`basestring`' proxy = original_association_proxy(*args, **kwargs) proxy.__doc__ = """ Internal. For use in SQLAlchemy queries. :type: {0} """.format(type_) return proxy
Example #30
Source File: resource_models_base.py From cloudify-manager with Apache License 2.0 | 5 votes |
def created_by(cls): return association_proxy('creator', 'username')