MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / _determine_joins

Method _determine_joins

lib/sqlalchemy/orm/relationships.py:2456–2549  ·  view source on GitHub ↗

Determine the 'primaryjoin' and 'secondaryjoin' attributes, if not passed to the constructor already. This is based on analysis of the foreign key relationships between the parent and target mapped selectables.

(self)

Source from the content-addressed store, hash-verified

2454 log.info("%s relationship direction %s", self.prop, self.direction)
2455
2456 def _determine_joins(self) -> None:
2457 """Determine the 'primaryjoin' and 'secondaryjoin' attributes,
2458 if not passed to the constructor already.
2459
2460 This is based on analysis of the foreign key relationships
2461 between the parent and target mapped selectables.
2462
2463 """
2464 if self.secondaryjoin is not None and self.secondary is None:
2465 raise sa_exc.ArgumentError(
2466 "Property %s specified with secondary "
2467 "join condition but "
2468 "no secondary argument" % self.prop
2469 )
2470
2471 # find a join between the given mapper's mapped table and
2472 # the given table. will try the mapper's local table first
2473 # for more specificity, then if not found will try the more
2474 # general mapped table, which in the case of inheritance is
2475 # a join.
2476 try:
2477 consider_as_foreign_keys = self.consider_as_foreign_keys or None
2478 if self.secondary is not None:
2479 if self.secondaryjoin is None:
2480 self.secondaryjoin = join_condition(
2481 self.child_persist_selectable,
2482 self.secondary,
2483 a_subset=self.child_local_selectable,
2484 consider_as_foreign_keys=consider_as_foreign_keys,
2485 )
2486 if self.primaryjoin_initial is None:
2487 self.primaryjoin = join_condition(
2488 self.parent_persist_selectable,
2489 self.secondary,
2490 a_subset=self.parent_local_selectable,
2491 consider_as_foreign_keys=consider_as_foreign_keys,
2492 )
2493 else:
2494 self.primaryjoin = self.primaryjoin_initial
2495 else:
2496 if self.primaryjoin_initial is None:
2497 self.primaryjoin = join_condition(
2498 self.parent_persist_selectable,
2499 self.child_persist_selectable,
2500 a_subset=self.parent_local_selectable,
2501 consider_as_foreign_keys=consider_as_foreign_keys,
2502 )
2503 else:
2504 self.primaryjoin = self.primaryjoin_initial
2505 except sa_exc.NoForeignKeysError as nfe:
2506 if self.secondary is not None:
2507 raise sa_exc.NoForeignKeysError(
2508 "Could not determine join "
2509 "condition between parent/child tables on "
2510 "relationship %s - there are no foreign keys "
2511 "linking these tables via secondary table '%s'. "
2512 "Ensure that referencing columns are associated "
2513 "with a ForeignKey or ForeignKeyConstraint, or "

Callers 1

__init__Method · 0.95

Calls 1

join_conditionFunction · 0.85

Tested by

no test coverage detected