Here are the examples of the python api sqlalchemy.orm.relationship taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
2081 Examples
3
Source : oauth.py
with GNU Affero General Public License v3.0
from closeio
with GNU Affero General Public License v3.0
from closeio
def secret(cls):
return relationship(
"Secret",
cascade="all",
uselist=False,
lazy="joined",
foreign_keys=[cls.refresh_token_id],
)
@property
3
Source : models.py
with Apache License 2.0
from CloudmindsRobot
with Apache License 2.0
from CloudmindsRobot
def slices(self) -> RelationshipProperty:
return relationship(
"Slice",
primaryjoin=lambda: and_(
foreign(Slice.datasource_id) == self.id,
foreign(Slice.datasource_type) == self.type,
),
)
# placeholder for a relationship to a derivative of BaseColumn
columns: List[Any] = []
3
Source : schedules.py
with Apache License 2.0
from CloudmindsRobot
with Apache License 2.0
from CloudmindsRobot
def user(self) -> RelationshipProperty:
return relationship(
security_manager.user_model,
backref=self.__tablename__,
foreign_keys=[self.user_id],
)
recipients = Column(Text)
3
Source : test_pagination.py
with MIT License
from dialoguemd
with MIT License
from dialoguemd
def user_cls(note_cls):
from fastapi_sqla import Base
class User(Base):
__tablename__ = "user"
notes = relationship("Note")
return User
@fixture(scope="module")
3
Source : abstract_models.py
with GNU General Public License v3.0
from dreamingspires
with GNU General Public License v3.0
from dreamingspires
def __new__(cls, clsname, bases, namespace, profile_table=None,
profile_table_name=None):
namespace['seller_id'] = Column(Text(),
ForeignKey(profile_table_name + '.id'))
namespace['winner_id'] = Column(Text(),
ForeignKey(profile_table_name + '.id'))
namespace['seller'] = relationship(profile_table, \
backref='auctions_sold', foreign_keys=clsname + '.seller_id')
namespace['winner'] = relationship(profile_table, \
backref='auctions_won', foreign_keys=clsname + '.winner_id')
return super(BaseAuctionRelationshipMeta, cls). \
__new__( cls, clsname, bases, namespace)
# Must be defined since DeclarativeBase apparently (unlike type)
# can't "handle extra keyword arguments gracefully"
# https://stackoverflow.com/questions/13762231/how-to-pass-arguments-to-the-metaclass-from-the-class-definition
def __init__(cls, clsname, bases, namespace, profile_table=None,
3
Source : table_per_association.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def addresses(cls):
address_association = Table(
"%s_addresses" % cls.__tablename__,
cls.metadata,
Column("address_id", ForeignKey("address.id"), primary_key=True),
Column(
"%s_id" % cls.__tablename__,
ForeignKey("%s.id" % cls.__tablename__),
primary_key=True,
),
)
return relationship(Address, secondary=address_association)
class Customer(HasAddresses, Base):
3
Source : table_per_related.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def addresses(cls):
cls.Address = type(
"%sAddress" % cls.__name__,
(Address, Base),
dict(
__tablename__="%s_address" % cls.__tablename__,
parent_id=Column(
Integer, ForeignKey("%s.id" % cls.__tablename__)
),
parent=relationship(cls),
),
)
return relationship(cls.Address)
class Customer(HasAddresses, Base):
3
Source : test_orm.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def setup_mappers(cls):
Child, Parent, parent, child = (
cls.classes.Child,
cls.classes.Parent,
cls.tables.parent,
cls.tables.child,
)
mapper(
Parent,
parent,
properties={"children": relationship(Child, backref="parent")},
)
mapper(Child, child)
@classmethod
3
Source : test_orm.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def setup_mappers(cls):
Child, Parent, parent, child = (
cls.classes.Child,
cls.classes.Parent,
cls.tables.parent,
cls.tables.child,
)
mapper(Parent, parent, properties={"child": relationship(Child)})
mapper(Child, child)
@classmethod
3
Source : test_orm.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def setup_mappers(cls):
A, B, C, D = cls.classes.A, cls.classes.B, cls.classes.C, cls.classes.D
a, b, c, d = cls.tables.a, cls.tables.b, cls.tables.c, cls.tables.d
mapper(
A,
a,
properties={
"bs": relationship(B, backref="a"),
"c": relationship(C, backref="as"),
"ds": relationship(D, backref="a"),
},
)
mapper(B, b)
mapper(C, c)
mapper(D, d)
@classmethod
3
Source : test_orm.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def setup_mappers(cls):
Child, Parent, parent, child = (
cls.classes.Child,
cls.classes.Parent,
cls.tables.parent,
cls.tables.child,
)
mapper(
Parent,
parent,
properties={"children": relationship(Child, backref="parent")},
)
mapper(Child, child)
def test_attribute_set(self):
3
Source : test_orm.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def setup_mappers(cls):
Child, Parent, parent, child = (
cls.classes.Child,
cls.classes.Parent,
cls.tables.parent,
cls.tables.child,
)
mapper(
Parent,
parent,
properties={"children": relationship(Child, backref="parent")},
)
mapper(Child, child)
def test_expire_lots(self):
3
Source : test_orm.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def setup_mappers(cls):
A, B, C = cls.classes("A", "B", "C")
a, b, c = cls.tables("a", "b", "c")
mapper(A, a, properties={"bs": relationship(B)})
mapper(B, b, properties={"cs": relationship(C)})
mapper(C, c)
@classmethod
3
Source : test_orm.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def setup_mappers(cls):
A, B, C, D, E, F, G = cls.classes("A", "B", "C", "D", "E", "F", "G")
a, b, c, d, e, f, g = cls.tables("a", "b", "c", "d", "e", "f", "g")
mapper(A, a, properties={"bs": relationship(B), "es": relationship(E)})
mapper(B, b, properties={"cs": relationship(C)})
mapper(C, c, properties={"ds": relationship(D)})
mapper(D, d)
mapper(E, e, properties={"fs": relationship(F), "gs": relationship(G)})
mapper(F, f)
mapper(G, g)
@classmethod
3
Source : test_basic.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_relationship_level_msg_for_invalid_callable(self):
class A(Base):
__tablename__ = "a"
id = Column(Integer, primary_key=True)
class B(Base):
__tablename__ = "b"
id = Column(Integer, primary_key=True)
a_id = Column(Integer, ForeignKey("a.id"))
a = relationship("a")
assert_raises_message(
sa.exc.ArgumentError,
"relationship 'a' expects a class or a mapper "
"argument .received: .*Table",
configure_mappers,
)
def test_relationship_level_msg_for_invalid_object(self):
3
Source : test_basic.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_relationship_level_msg_for_invalid_object(self):
class A(Base):
__tablename__ = "a"
id = Column(Integer, primary_key=True)
class B(Base):
__tablename__ = "b"
id = Column(Integer, primary_key=True)
a_id = Column(Integer, ForeignKey("a.id"))
a = relationship(A.__table__)
assert_raises_message(
sa.exc.ArgumentError,
"relationship 'a' expects a class or a mapper "
"argument .received: .*Table",
configure_mappers,
)
def test_difficult_class(self):
3
Source : test_basic.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_shared_class_registry(self):
reg = {}
Base1 = decl.declarative_base(testing.db, class_registry=reg)
Base2 = decl.declarative_base(testing.db, class_registry=reg)
class A(Base1):
__tablename__ = "a"
id = Column(Integer, primary_key=True)
class B(Base2):
__tablename__ = "b"
id = Column(Integer, primary_key=True)
aid = Column(Integer, ForeignKey(A.id))
as_ = relationship("A")
assert B.as_.property.mapper.class_ is A
def test_uncompiled_attributes_in_relationship(self):
3
Source : test_basic.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_nice_dependency_error(self):
class User(Base):
__tablename__ = "users"
id = Column("id", Integer, primary_key=True)
addresses = relationship("Address")
class Address(Base):
__tablename__ = "addresses"
id = Column(Integer, primary_key=True)
foo = sa.orm.column_property(User.id == 5)
# this used to raise an error when accessing User.id but that's
# no longer the case since we got rid of _CompileOnAttr.
assert_raises(sa.exc.ArgumentError, configure_mappers)
def test_nice_dependency_error_works_with_hasattr(self):
3
Source : test_basic.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_alt_name_attr_subclass_relationship_attrset(self):
# [ticket:2900]
class A(Base):
__tablename__ = "a"
id = Column("id", Integer, primary_key=True)
b_id = Column(Integer, ForeignKey("b.id"))
b = relationship("B", backref="as_")
A.brap = A.b
class B(Base):
__tablename__ = "b"
id = Column("id", Integer, primary_key=True)
assert A.brap.property is A.b.property
assert isinstance(A.brap.original_property, properties.SynonymProperty)
A(brap=B())
def test_eager_order_by(self):
3
Source : test_concurrency.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def make_a(cls, Base):
class A(Base):
__tablename__ = "a"
id = Column(Integer, primary_key=True)
data = Column(String)
bs = relationship("B")
# need a strong ref so that the class is not gc'ed
cls.A = A
@classmethod
3
Source : test_reflection.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_pk_fk(self):
class B(decl.DeferredReflection, fixtures.ComparableEntity, Base):
__tablename__ = "b"
a = relationship("A")
class A(decl.DeferredReflection, fixtures.ComparableEntity, Base):
__tablename__ = "a"
decl.DeferredReflection.prepare(testing.db)
class DeferredReflectionTest(DeferredReflectBase):
3
Source : test_reflection.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_basic_deferred(self):
class User(decl.DeferredReflection, fixtures.ComparableEntity, Base):
__tablename__ = "users"
addresses = relationship("Address", backref="user")
class Address(
decl.DeferredReflection, fixtures.ComparableEntity, Base
):
__tablename__ = "addresses"
decl.DeferredReflection.prepare(testing.db)
self._roundtrip()
def test_abstract_base(self):
3
Source : test_reflection.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_redefine_fk_double(self):
class User(decl.DeferredReflection, fixtures.ComparableEntity, Base):
__tablename__ = "users"
addresses = relationship("Address", backref="user")
class Address(
decl.DeferredReflection, fixtures.ComparableEntity, Base
):
__tablename__ = "addresses"
user_id = Column(Integer, ForeignKey("users.id"))
decl.DeferredReflection.prepare(testing.db)
self._roundtrip()
def test_mapper_args_deferred(self):
3
Source : test_reflection.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_string_resolution(self):
class User(decl.DeferredReflection, fixtures.ComparableEntity, Base):
__tablename__ = "users"
items = relationship("Item", secondary="user_items")
class Item(decl.DeferredReflection, fixtures.ComparableEntity, Base):
__tablename__ = "items"
decl.DeferredReflection.prepare(testing.db)
self._roundtrip()
def test_table_resolution(self):
3
Source : test_reflection.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_table_resolution(self):
class User(decl.DeferredReflection, fixtures.ComparableEntity, Base):
__tablename__ = "users"
items = relationship(
"Item", secondary=Table("user_items", Base.metadata)
)
class Item(decl.DeferredReflection, fixtures.ComparableEntity, Base):
__tablename__ = "items"
decl.DeferredReflection.prepare(testing.db)
self._roundtrip()
class DeferredInhReflectBase(DeferredReflectBase):
3
Source : test_associationproxy.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_weak_identity_map(self):
mapper(
Parent, self.parents, properties=dict(children=relationship(Child))
)
mapper(Child, self.children)
session = create_session()
def add_child(parent_name, child_name):
parent = session.query(Parent).filter_by(name=parent_name).one()
parent.kids.append(child_name)
add_child("p1", "c1")
gc_collect()
add_child("p1", "c2")
session.flush()
p = session.query(Parent).filter_by(name="p1").one()
assert set(p.kids) == set(["c1", "c2"]), p.kids
def test_copy(self):
3
Source : test_associationproxy.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_copy(self):
mapper(
Parent, self.parents, properties=dict(children=relationship(Child))
)
mapper(Child, self.children)
p = Parent("p1")
p.kids.extend(["c1", "c2"])
p_copy = copy.copy(p)
del p
gc_collect()
assert set(p_copy.kids) == set(["c1", "c2"]), p_copy.kids
def test_pickle_list(self):
3
Source : test_associationproxy.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_pickle_list(self):
mapper(
Parent, self.parents, properties=dict(children=relationship(Child))
)
mapper(Child, self.children)
p = Parent("p1")
p.kids.extend(["c1", "c2"])
r1 = pickle.loads(pickle.dumps(p))
assert r1.kids == ["c1", "c2"]
# can't do this without parent having a cycle
# r2 = pickle.loads(pickle.dumps(p.kids))
# assert r2 == ['c1', 'c2']
def test_pickle_set(self):
3
Source : test_associationproxy.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_pickle_set(self):
mapper(
Parent,
self.parents,
properties=dict(
children=relationship(Child, collection_class=set)
),
)
mapper(Child, self.children)
p = Parent("p1")
p.kids.update(["c1", "c2"])
r1 = pickle.loads(pickle.dumps(p))
assert r1.kids == set(["c1", "c2"])
# can't do this without parent having a cycle
# r2 = pickle.loads(pickle.dumps(p.kids))
# assert r2 == set(['c1', 'c2'])
def test_pickle_dict(self):
3
Source : test_automap.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_relationship_explicit_override_o2m(self):
Base = automap_base(metadata=self.metadata)
prop = relationship("addresses", collection_class=set)
class User(Base):
__tablename__ = "users"
addresses_collection = prop
Base.prepare()
assert User.addresses_collection.property is prop
Address = Base.classes.addresses
a1 = Address(email_address="e1")
u1 = User(name="u1", addresses_collection=set([a1]))
assert a1.user is u1
def test_exception_prepare_not_called(self):
3
Source : test_automap.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_relationship_explicit_override_m2o(self):
Base = automap_base(metadata=self.metadata)
prop = relationship("users")
class Address(Base):
__tablename__ = "addresses"
users = prop
Base.prepare()
User = Base.classes.users
assert Address.users.property is prop
a1 = Address(email_address="e1")
u1 = User(name="u1", address_collection=[a1])
assert a1.users is u1
def test_relationship_self_referential(self):
3
Source : test_baked.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def setup_mappers(cls):
User = cls.classes.User
Address = cls.classes.Address
Order = cls.classes.Order
mapper(
User,
cls.tables.users,
properties={
"addresses": relationship(
Address, order_by=cls.tables.addresses.c.id
),
"orders": relationship(Order, order_by=cls.tables.orders.c.id),
},
)
mapper(Address, cls.tables.addresses)
mapper(Order, cls.tables.orders)
@contextlib.contextmanager
3
Source : test_baked.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def setup_mappers(cls):
User = cls.classes.User
Address = cls.classes.Address
Order = cls.classes.Order
mapper(
User,
cls.tables.users,
properties={
"addresses": relationship(
Address, order_by=cls.tables.addresses.c.id
),
"orders": relationship(Order, order_by=cls.tables.orders.c.id),
},
)
mapper(Address, cls.tables.addresses)
mapper(Order, cls.tables.orders)
def test_cachekeys_on_constructor(self):
3
Source : test_baked.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def _m2o_fixture(self):
User = self.classes.User
Address = self.classes.Address
mapper(User, self.tables.users)
mapper(
Address,
self.tables.addresses,
properties={"user": relationship(User)},
)
return User, Address
def test_no_cache_for_event(self, modify_query_fixture):
3
Source : test_horizontal_shard.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def setup_classes(cls):
Base = cls.DeclarativeBasic
class Book(Base):
__tablename__ = "book"
id = Column(Integer, primary_key=True)
pages = relationship("Page")
class Page(Base):
__tablename__ = "page"
id = Column(Integer, primary_key=True)
book_id = Column(ForeignKey("book.id"))
def test_selectinload_query(self):
3
Source : test_horizontal_shard.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def setup_classes(cls):
Base = cls.DeclarativeBasic
class Book(Base):
__tablename__ = "book"
id = Column(Integer, primary_key=True)
title = Column(String(50), nullable=False)
pages = relationship("Page", backref="book")
class Page(Base):
__tablename__ = "page"
id = Column(Integer, primary_key=True)
book_id = Column(ForeignKey("book.id"))
title = Column(String(50))
def _fixture(self, lazy_load_book=False, lazy_load_pages=False):
3
Source : test_serializer.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def setup_mappers(cls):
global Session
Session = scoped_session(sessionmaker())
mapper(
User,
users,
properties={
"addresses": relationship(
Address, backref="user", order_by=addresses.c.id
)
},
)
mapper(Address, addresses)
configure_mappers()
@classmethod
3
Source : test_relationship.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def _fixture_from_base(self):
Parent = self.classes.Parent
Link = self.classes.Link
Link.child = relationship(
Parent, primaryjoin=Link.child_id == Parent.id
)
Parent.links = relationship(
Link, primaryjoin=Parent.id == Link.parent_id
)
return Parent
def _fixture_from_subclass(self):
3
Source : test_relationship.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def _fixture_from_subclass(self):
Sub1 = self.classes.Sub1
Link = self.classes.Link
Parent = self.classes.Parent
Link.child = relationship(
Parent, primaryjoin=Link.child_id == Parent.id
)
Sub1.links = relationship(Link, primaryjoin=Sub1.id == Link.parent_id)
return Sub1
def _fixture_to_subclass_to_base(self):
3
Source : test_relationship.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def _fixture_to_subclass_to_base(self):
Owner = self.classes.Owner
Parent = self.classes.Parent
Sub1 = self.classes.Sub1
Link = self.classes.Link
# Link -> Sub1 -> Owner
Link.child = relationship(Sub1, primaryjoin=Link.child_id == Sub1.id)
Parent.owner_id = Column(ForeignKey("owner.id"))
Parent.owner = relationship(Owner)
return Parent
def _fixture_to_base_to_base(self):
3
Source : test_relationship.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def _fixture_to_base_to_base(self):
Owner = self.classes.Owner
Parent = self.classes.Parent
Link = self.classes.Link
# Link -> Parent -> Owner
Link.child = relationship(
Parent, primaryjoin=Link.child_id == Parent.id
)
Parent.owner_id = Column(ForeignKey("owner.id"))
Parent.owner = relationship(Owner)
return Parent
def test_from_base(self):
3
Source : test_backref_mutations.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
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")),
)
def test_collection_move_hitslazy(self):
3
Source : test_backref_mutations.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
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)},
)
def test_collection_move_commitfirst(self):
3
Source : test_backref_mutations.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
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")),
)
def test_backref_pop_m2o(self):
3
Source : test_bundle.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def setup_mappers(cls):
mapper(
cls.classes.Data,
cls.tables.data,
properties={"others": relationship(cls.classes.Other)},
)
mapper(cls.classes.Other, cls.tables.other)
@classmethod
3
Source : test_cascade.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_cascade_unicode(self):
Address = self.classes.Address
rel = relationship(Address)
rel.cascade = util.u("save-update, merge, expunge")
eq_(rel.cascade, set(["save-update", "merge", "expunge"]))
class O2MCascadeDeleteOrphanTest(fixtures.MappedTest):
3
Source : test_cascade.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def setup_mappers(cls):
users, User, Address, addresses = (
cls.tables.users,
cls.classes.User,
cls.classes.Address,
cls.tables.addresses,
)
mapper(Address, addresses)
mapper(
User,
users,
properties={"addresses": relationship(Address, backref="user")},
)
def test_none_o2m_collection_assignment(self):
3
Source : test_cascade.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def setup_mappers(cls):
User, Order, orders, users = (
cls.classes.User,
cls.classes.Order,
cls.tables.orders,
cls.tables.users,
)
mapper(
User,
users,
properties=dict(
orders=relationship(mapper(Order, orders), cascade="all")
),
)
def test_cascade_delete_noorphans(self):
3
Source : test_cascade.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def setup_mappers(cls):
t2, T2, T3, t1, t3, T1 = (
cls.tables.t2,
cls.classes.T2,
cls.classes.T3,
cls.tables.t1,
cls.tables.t3,
cls.classes.T1,
)
mapper(T1, t1, properties={"t2": relationship(T2, cascade="all")})
mapper(T2, t2, properties={"t3": relationship(T3, cascade="all")})
mapper(T3, t3)
def test_cascade_delete(self):
3
Source : test_cascade.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def setup_mappers(cls):
Node = cls.classes.Node
node = cls.tables.node
mapper(
Node,
node,
properties={
"children": relationship(
Node,
cascade="all, delete-orphan",
backref=backref("parent", remote_side=node.c.id),
)
},
)
def test_self_referential_delete(self):
See More Examples