Python sqlalchemy.orm.backref() Examples
The following are 30
code examples of sqlalchemy.orm.backref().
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_backref_mutations.py From sqlalchemy with MIT License | 6 votes |
def test_scalar_move_preloaded(self): User, Address = self.classes.User, self.classes.Address sess = sessionmaker()() a1 = Address(email_address="address1") a2 = Address(email_address="address1") u1 = User(name="jack", address=a1) sess.add_all([u1, a1, a2]) sess.commit() # everything is expired # load a1.user a1.user # reassign a2.user = u1 # backref fires assert u1.address is a2 # stays on both sides assert a1.user is u1 assert a2.user is u1
Example #2
Source File: test_cascade.py From sqlalchemy with MIT License | 6 votes |
def setup_mappers(cls): Address, addresses, users, User = ( cls.classes.Address, cls.tables.addresses, cls.tables.users, cls.classes.User, ) mapper(Address, addresses) mapper( User, users, properties={ "address": relationship( Address, backref=backref("user", single_parent=True), uselist=False, ) }, )
Example #3
Source File: test_update_delete.py From sqlalchemy with MIT License | 6 votes |
def setup_mappers(cls): documents, Document, User, users = ( cls.tables.documents, cls.classes.Document, cls.classes.User, cls.tables.users, ) mapper(User, users) mapper( Document, documents, properties={ "user": relationship( User, lazy="joined", backref=backref("documents", lazy="select"), ) }, )
Example #4
Source File: test_backref_mutations.py From sqlalchemy with MIT License | 6 votes |
def setup_mappers(cls): keywords, items, item_keywords, Keyword, Item = ( cls.tables.keywords, cls.tables.items, cls.tables.item_keywords, cls.classes.Keyword, cls.classes.Item, ) mapper( Item, items, properties={ "keywords": relationship( Keyword, secondary=item_keywords, backref="items" ) }, ) mapper(Keyword, keywords)
Example #5
Source File: test_backref_mutations.py From sqlalchemy with MIT License | 6 votes |
def setup_mappers(cls): keywords, items, item_keywords, Keyword, Item = ( cls.tables.keywords, cls.tables.items, cls.tables.item_keywords, cls.classes.Keyword, cls.classes.Item, ) mapper( Item, items, properties={ "keyword": relationship( Keyword, secondary=item_keywords, uselist=False, backref=backref("item", uselist=False), ) }, ) mapper(Keyword, keywords)
Example #6
Source File: test_backref_mutations.py From sqlalchemy with MIT License | 6 votes |
def test_m2o_event(self): User, Address = self.classes.User, self.classes.Address sess = sessionmaker()() a1 = Address(email_address="address1") u1 = User(name="jack", address=a1) sess.add(u1) sess.commit() sess.expunge(u1) u2 = User(name="ed") # the _SingleParent extension sets the backref get to "active" ! # u1 gets loaded and deleted u2.address = a1 sess.commit() assert sess.query(User).count() == 1
Example #7
Source File: test_cascade.py From sqlalchemy with MIT License | 6 votes |
def setup_mappers(cls): Address, addresses, users, User = ( cls.classes.Address, cls.tables.addresses, cls.tables.users, cls.classes.User, ) mapper(Address, addresses) mapper( User, users, properties={ "address": relationship( Address, backref=backref( "user", single_parent=True, cascade="all, delete-orphan", ), uselist=False, ) }, )
Example #8
Source File: test_backref_mutations.py From sqlalchemy with MIT License | 6 votes |
def setup_mappers(cls): Address, addresses, users, User = ( cls.classes.Address, cls.tables.addresses, cls.tables.users, cls.classes.User, ) mapper(Address, addresses) mapper( User, users, properties={ "address": relationship( Address, uselist=False, backref=backref( "user", single_parent=True, cascade="all, delete-orphan", ), ) }, )
Example #9
Source File: test_backref_mutations.py From sqlalchemy with MIT License | 6 votes |
def test_scalar_move_notloaded(self): User, Address = self.classes.User, self.classes.Address sess = sessionmaker()() a1 = Address(email_address="address1") a2 = Address(email_address="address1") u1 = User(name="jack", address=a1) sess.add_all([u1, a1, a2]) sess.commit() # everything is expired # reassign a2.user = u1 # backref fires assert u1.address is a2 # stays on both sides assert a1.user is u1 assert a2.user is u1
Example #10
Source File: test_backref_mutations.py From sqlalchemy with MIT License | 6 votes |
def test_collection_move_preloaded(self): User, Address = self.classes.User, self.classes.Address sess = sessionmaker()() a1 = Address(email_address="address1") u1 = User(name="jack", address=a1) u2 = User(name="ed") sess.add_all([u1, u2]) sess.commit() # everything is expired # load u1.address u1.address # reassign u2.address = a1 assert u2.address is a1 # backref fires assert a1.user is u2 # doesn't extend to the previous attribute tho. # flushing at this point means its anyone's guess. assert u1.address is a1 assert u2.address is a1
Example #11
Source File: test_backref_mutations.py From sqlalchemy with MIT License | 6 votes |
def setup_mappers(cls): Address, addresses, users, User = ( cls.classes.Address, cls.tables.addresses, cls.tables.users, cls.classes.User, ) mapper(Address, addresses) mapper( User, users, properties={ "address": relationship( Address, backref=backref("user"), uselist=False ) }, )
Example #12
Source File: test_backref_mutations.py From sqlalchemy with MIT License | 6 votes |
def test_scalar_move_commitfirst(self): User, Address = self.classes.User, self.classes.Address sess = sessionmaker()() u1 = User(name="jack") u2 = User(name="ed") a1 = Address(email_address="a1") a1.user = u1 sess.add_all([u1, u2, a1]) sess.commit() # u1.addresses is loaded u1.addresses # direct set - the fetching of the # "old" u1 here allows the backref # to remove it from the addresses collection a1.user = u2 sess.commit() assert a1 not in u1.addresses assert a1 in u2.addresses
Example #13
Source File: test_backref_mutations.py From sqlalchemy with MIT License | 6 votes |
def test_scalar_move_notloaded(self): User, Address = self.classes.User, self.classes.Address sess = sessionmaker()() u1 = User(name="jack") u2 = User(name="ed") a1 = Address(email_address="a1") a1.user = u1 sess.add_all([u1, u2, a1]) sess.commit() # direct set - the fetching of the # "old" u1 here allows the backref # to remove it from the addresses collection a1.user = u2 assert a1 not in u1.addresses assert a1 in u2.addresses
Example #14
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 #15
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 #16
Source File: test_backref_mutations.py From sqlalchemy with MIT License | 6 votes |
def test_collection_move_notloaded(self): User, Address = self.classes.User, self.classes.Address sess = sessionmaker()() a1 = Address(email_address="address1") u1 = User(name="jack", addresses=[a1]) u2 = User(name="ed") sess.add_all([u1, u2]) sess.commit() # everything is expired u2.addresses.append(a1) # backref fires assert a1.user is u2 # u1.addresses wasn't loaded, # so when it loads its correct assert a1 not in u1.addresses assert a1 in u2.addresses
Example #17
Source File: test_backref_mutations.py From sqlalchemy with MIT License | 6 votes |
def test_collection_move_preloaded(self): User, Address = self.classes.User, self.classes.Address sess = sessionmaker()() a1 = Address(email_address="address1") u1 = User(name="jack", addresses=[a1]) u2 = User(name="ed") sess.add_all([u1, u2]) sess.commit() # everything is expired # load u1.addresses collection u1.addresses u2.addresses.append(a1) # backref fires assert a1.user is u2 # a1 removed from u1.addresses as of [ticket:2789] assert a1 not in u1.addresses assert a1 in u2.addresses
Example #18
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 #19
Source File: test_manytomany.py From sqlalchemy with MIT License | 6 votes |
def _standard_bidirectional_fixture(self): left, secondary, right = ( self.tables.left, self.tables.secondary, self.tables.right, ) A, B = self.classes.A, self.classes.B mapper( A, left, properties={ "bs": relationship( B, secondary=secondary, backref="as", order_by=right.c.id ) }, ) mapper(B, right)
Example #20
Source File: test_backref_mutations.py From sqlalchemy with MIT License | 5 votes |
def setup_mappers(cls): Address, addresses, users, User = ( cls.classes.Address, cls.tables.addresses, cls.tables.users, cls.classes.User, ) mapper(Address, addresses) mapper( User, users, properties=dict(addresses=relationship(Address, backref="user")), )
Example #21
Source File: test_relationship.py From sqlalchemy with MIT License | 5 votes |
def setup_mappers(cls): child1 = cls.tables.child1 child2 = cls.tables.child2 Parent = cls.classes.Parent parent = cls.tables.parent Child1 = cls.classes.Child1 Child2 = cls.classes.Child2 secondary = cls.tables.secondary mapper(Parent, parent, polymorphic_on=parent.c.cls) mapper( Child1, child1, inherits=Parent, polymorphic_identity="child1", properties={ "left_child2": relationship( Child2, secondary=secondary, primaryjoin=parent.c.id == secondary.c.right_id, secondaryjoin=parent.c.id == secondary.c.left_id, uselist=False, backref="right_children", ) }, ) mapper(Child2, child2, inherits=Parent, polymorphic_identity="child2")
Example #22
Source File: test_relationship.py From sqlalchemy with MIT License | 5 votes |
def setup_mappers(cls): organizations = cls.tables.organizations people = cls.tables.people engineers = cls.tables.engineers engineers_to_org = cls.tables.engineers_to_org class Organization(cls.Comparable): pass mapper( Organization, organizations, properties={ "engineers": relationship( Engineer, secondary=engineers_to_org, backref="organizations", ) }, ) mapper( Person, people, polymorphic_on=people.c.type, polymorphic_identity="person", ) mapper( Engineer, engineers, inherits=Person, polymorphic_identity="engineer", )
Example #23
Source File: test_relationship.py From sqlalchemy with MIT License | 5 votes |
def setup_mappers(cls): engineers = cls.tables.engineers people = cls.tables.people mapper( Person, people, polymorphic_on=people.c.type, polymorphic_identity="person", ) mapper( Engineer, engineers, inherits=Person, polymorphic_identity="engineer", properties={ "reports_to": relationship( Engineer, primaryjoin=( engineers.c.person_id == engineers.c.reports_to_id ), backref="engineers", remote_side=engineers.c.person_id, ) }, )
Example #24
Source File: test_relationship.py From sqlalchemy with MIT License | 5 votes |
def setup_mappers(cls): engineers = cls.tables.engineers managers = cls.tables.managers people = cls.tables.people mapper( Person, people, polymorphic_on=people.c.type, polymorphic_identity="person", ) mapper( Manager, managers, inherits=Person, polymorphic_identity="manager" ) mapper( Engineer, engineers, inherits=Person, polymorphic_identity="engineer", properties={ "reports_to": relationship( Manager, primaryjoin=( managers.c.person_id == engineers.c.reports_to_id ), backref="engineers", ) }, )
Example #25
Source File: test_eager_relations.py From sqlalchemy with MIT License | 5 votes |
def test_basic(self): t2, t1 = self.tables.t2, self.tables.t1 class T(object): pass class SubT(T): pass class T2(object): pass class SubT2(T2): pass mapper(T, t1, polymorphic_on=t1.c.type, polymorphic_identity="t1") mapper( SubT, None, inherits=T, polymorphic_identity="subt1", properties={ "t2s": relationship( SubT2, lazy="joined", backref=sa.orm.backref("subt", lazy="joined"), ) }, ) mapper(T2, t2, polymorphic_on=t2.c.type, polymorphic_identity="t2") mapper(SubT2, None, inherits=T2, polymorphic_identity="subt2") # testing a particular endless loop condition in eager load setup create_session().query(SubT).all()
Example #26
Source File: test_assorted_eager.py From sqlalchemy with MIT License | 5 votes |
def setup_mappers(cls): Account, Transaction, transactions, accounts, entries, Entry = ( cls.classes.Account, cls.classes.Transaction, cls.tables.transactions, cls.tables.accounts, cls.tables.entries, cls.classes.Entry, ) mapper(Account, accounts) mapper(Transaction, transactions) mapper( Entry, entries, properties=dict( account=relationship( Account, uselist=False, backref=backref( "entries", lazy="select", order_by=entries.c.entry_id ), ), transaction=relationship( Transaction, uselist=False, backref=backref( "entries", lazy="joined", order_by=entries.c.entry_id ), ), ), )
Example #27
Source File: test_eager_relations.py From sqlalchemy with MIT License | 5 votes |
def _fixture(self, props): A, B = self.classes.A, self.classes.B b_table, a_table = self.tables.b, self.tables.a mapper(A, a_table, properties=props) mapper(B, b_table, properties={"a": relationship(A, backref="bs")})
Example #28
Source File: test_eager_relations.py From sqlalchemy with MIT License | 5 votes |
def test_cyclical(self): """A circular eager relationship breaks the cycle with a lazy loader""" Address, addresses, users, User = ( self.classes.Address, self.tables.addresses, self.tables.users, self.classes.User, ) mapper(Address, addresses) mapper( User, users, properties=dict( addresses=relationship( Address, lazy="joined", backref=sa.orm.backref("user", lazy="joined"), order_by=Address.id, ) ), ) eq_(sa.orm.class_mapper(User).get_property("addresses").lazy, "joined") eq_(sa.orm.class_mapper(Address).get_property("user").lazy, "joined") sess = create_session() eq_( self.static.user_address_result, sess.query(User).order_by(User.id).all(), )
Example #29
Source File: mixins.py From sqlalchemy_mptt with MIT License | 5 votes |
def parent(self): return relationship( self, order_by=lambda: self.left, foreign_keys=[self.parent_id], remote_side="{}.{}".format(self.__name__, self.get_pk_name()), backref=backref( "children", cascade="all,delete", order_by=lambda: (self.tree_id, self.left), ), )
Example #30
Source File: test_backref_mutations.py From sqlalchemy with MIT License | 5 votes |
def setup_mappers(cls): Address, addresses, users, User = ( cls.classes.Address, cls.tables.addresses, cls.tables.users, cls.classes.User, ) mapper(Address, addresses) mapper( User, users, properties=dict(addresses=relationship(Address, backref="user")), )