本文整理汇总了Python中sqlalchemy.orm.foreign方法的典型用法代码示例。如果您正苦于以下问题:Python orm.foreign方法的具体用法?Python orm.foreign怎么用?Python orm.foreign使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.orm
的用法示例。
在下文中一共展示了orm.foreign方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_viewonly_warning
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import foreign [as 别名]
def test_viewonly_warning(self, flag, value):
Order = self.classes.Order
with testing.expect_warnings(
r"Setting %s on relationship\(\) while also setting "
"viewonly=True does not make sense" % flag
):
kw = {
"viewonly": True,
"primaryjoin": self.tables.users.c.id
== foreign(self.tables.orders.c.user_id),
}
kw[flag] = value
rel = relationship(Order, **kw)
eq_(getattr(rel, flag), value)
示例2: _join_fixture_remote_local_multiple_ref
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import foreign [as 别名]
def _join_fixture_remote_local_multiple_ref(self, **kw):
def fn(a, b):
return (a == b) | (b == a)
return relationships.JoinCondition(
self.selfref,
self.selfref,
self.selfref,
self.selfref,
support_sync=False,
primaryjoin=fn(
# we're putting a do-nothing annotation on
# "a" so that the left/right is preserved;
# annotation vs. non seems to affect __eq__ behavior
self.selfref.c.sid._annotate({"foo": "bar"}),
foreign(remote(self.selfref.c.sid)),
),
)
示例3: _assert_raises_no_equality
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import foreign [as 别名]
def _assert_raises_no_equality(
self, fn, expr, relname, primary, *arg, **kw
):
assert_raises_message(
exc.ArgumentError,
"Could not locate any simple equality expressions "
"involving locally mapped foreign key columns for %s join "
"condition '%s' on relationship %s. "
"Ensure that referencing columns are associated with a "
"ForeignKey or ForeignKeyConstraint, or are annotated in "
r"the join condition with the foreign\(\) annotation. "
"To allow comparison operators other than '==', "
"the relationship can be marked as viewonly=True."
% (primary, expr, relname),
fn,
*arg,
**kw
)
示例4: test_determine_join_ambiguous_fks_o2m
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import foreign [as 别名]
def test_determine_join_ambiguous_fks_o2m(self):
assert_raises_message(
exc.AmbiguousForeignKeysError,
"Could not determine join condition between "
"parent/child tables on relationship None - "
"there are multiple foreign key paths linking "
"the tables. Specify the 'foreign_keys' argument, "
"providing a list of those columns which "
"should be counted as containing a foreign "
"key reference to the parent table.",
relationships.JoinCondition,
self.left,
self.right_multi_fk,
self.left,
self.right_multi_fk,
)
示例5: test_pj_deannotates
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import foreign [as 别名]
def test_pj_deannotates(self):
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
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(ForeignKey(A.id))
a = relationship(A)
eq_(
B.a.property.primaryjoin.left._annotations,
{"parentmapper": A.__mapper__, "remote": True},
)
eq_(
B.a.property.primaryjoin.right._annotations,
{"foreign": True, "local": True, "parentmapper": B.__mapper__},
)
示例6: test_write_cascade_disallowed_w_viewonly
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import foreign [as 别名]
def test_write_cascade_disallowed_w_viewonly(self):
Order = self.classes.Order
assert_raises_message(
sa_exc.ArgumentError,
'Cascade settings "delete, delete-orphan, merge, save-update" '
"apply to persistence operations",
relationship,
Order,
primaryjoin=(
self.tables.users.c.id == foreign(self.tables.orders.c.user_id)
),
cascade="all, delete, delete-orphan",
viewonly=True,
)
示例7: _assert_raises_no_equality
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import foreign [as 别名]
def _assert_raises_no_equality(
self, fn, expr, relname, primary, *arg, **kw
):
assert_raises_message(
sa.exc.ArgumentError,
"Could not locate any simple equality expressions "
"involving locally mapped foreign key columns for %s join "
"condition '%s' on relationship %s. "
"Ensure that referencing columns are associated with a "
"ForeignKey or ForeignKeyConstraint, or are annotated in "
r"the join condition with the foreign\(\) annotation. "
"To allow comparison operators other than '==', "
"the relationship can be marked as viewonly=True."
% (primary, expr, relname),
fn,
*arg,
**kw
)
示例8: _descendants_fixture
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import foreign [as 别名]
def _descendants_fixture(self, data=True):
Entity = self.classes.Entity
entity = self.tables.entity
m = mapper(
Entity,
entity,
properties={
"descendants": relationship(
Entity,
primaryjoin=remote(foreign(entity.c.path)).like(
entity.c.path.concat("/%")
),
viewonly=True,
order_by=entity.c.path,
)
},
)
configure_mappers()
assert m.get_property("descendants").direction is ONETOMANY
if data:
return self._fixture()
示例9: _anscestors_fixture
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import foreign [as 别名]
def _anscestors_fixture(self, data=True):
Entity = self.classes.Entity
entity = self.tables.entity
m = mapper(
Entity,
entity,
properties={
"anscestors": relationship(
Entity,
primaryjoin=entity.c.path.like(
remote(foreign(entity.c.path)).concat("/%")
),
viewonly=True,
order_by=entity.c.path,
)
},
)
configure_mappers()
assert m.get_property("anscestors").direction is ONETOMANY
if data:
return self._fixture()
示例10: _test_no_overwrite
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import foreign [as 别名]
def _test_no_overwrite(self, sess, expect_failure):
# test [ticket:3230]
Employee, Company = self.classes.Employee, self.classes.Company
c1 = sess.query(Company).filter_by(name="c1").one()
e3 = sess.query(Employee).filter_by(name="emp3").one()
e3.reports_to = None
if expect_failure:
# if foreign() isn't applied specifically to
# employee_t.c.reports_to_id only, then
# employee_t.c.company_id goes foreign as well and then
# this happens
assert_raises_message(
AssertionError,
"Dependency rule tried to blank-out primary key column "
"'employee_t.company_id'",
sess.flush,
)
else:
sess.flush()
eq_(e3.company, c1)
示例11: setup_classes
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import foreign [as 别名]
def setup_classes(cls):
Base = cls.DeclarativeBasic
class Venue(Base):
__tablename__ = "venue"
id = Column(Integer, primary_key=True)
name = Column(String)
descendants = relationship(
"Venue",
primaryjoin=func.instr(
remote(foreign(name)), name + "/"
).as_comparison(1, 2)
== 1,
viewonly=True,
order_by=name,
)
示例12: test_onetomany_funcfk_annotated
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import foreign [as 别名]
def test_onetomany_funcfk_annotated(self):
T2, T1, t2, t1 = (
self.classes.T2,
self.classes.T1,
self.tables.t2,
self.tables.t1,
)
# use annotation
mapper(
T1,
t1,
properties={
"t2s": relationship(
T2,
primaryjoin=t1.c.id == foreign(sa.func.lower(t2.c.t1id)),
)
},
)
mapper(T2, t2)
self._test_onetomany()
示例13: _resolver
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import foreign [as 别名]
def _resolver(cls, prop):
import sqlalchemy
from sqlalchemy.orm import foreign, remote
fallback = sqlalchemy.__dict__.copy()
fallback.update({'foreign': foreign, 'remote': remote})
def resolve_arg(arg):
return _class_resolver(cls, prop, fallback, arg)
return resolve_arg
示例14: setup_listener
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import foreign [as 别名]
def setup_listener(mapper, class_):
name = class_.__name__
discriminator = name.lower()
class_.addresses = relationship(
Address,
primaryjoin=and_(
class_.id == foreign(remote(Address.parent_id)),
Address.discriminator == discriminator,
),
backref=backref(
"parent_%s" % discriminator,
primaryjoin=remote(class_.id) == foreign(Address.parent_id),
),
)
@event.listens_for(class_.addresses, "append")
def append_address(target, value, initiator):
value.discriminator = discriminator
示例15: _resolver
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import foreign [as 别名]
def _resolver(cls, prop):
import sqlalchemy
from sqlalchemy.orm import foreign, remote
fallback = sqlalchemy.__dict__.copy()
fallback.update({"foreign": foreign, "remote": remote})
def resolve_arg(arg):
return _class_resolver(cls, prop, fallback, arg)
def resolve_name(arg):
return _class_resolver(cls, prop, fallback, arg)._resolve_name
return resolve_name, resolve_arg