| 706 | parent_property: RelationshipProperty[Any] |
| 707 | |
| 708 | def __init__( |
| 709 | self, parent: RelationshipProperty[Any], strategy_key: Tuple[Any, ...] |
| 710 | ): |
| 711 | super().__init__(parent, strategy_key) |
| 712 | self._raise_always = self.strategy_opts["lazy"] == "raise" |
| 713 | self._raise_on_sql = self.strategy_opts["lazy"] == "raise_on_sql" |
| 714 | |
| 715 | self.is_aliased_class = inspect(self.entity).is_aliased_class |
| 716 | |
| 717 | join_condition = self.parent_property._join_condition |
| 718 | ( |
| 719 | self._lazywhere, |
| 720 | self._bind_to_col, |
| 721 | self._equated_columns, |
| 722 | ) = join_condition.create_lazy_clause() |
| 723 | |
| 724 | ( |
| 725 | self._rev_lazywhere, |
| 726 | self._rev_bind_to_col, |
| 727 | self._rev_equated_columns, |
| 728 | ) = join_condition.create_lazy_clause(reverse_direction=True) |
| 729 | |
| 730 | if self.parent_property.order_by: |
| 731 | self._order_by = [ |
| 732 | sql_util._deep_annotate(elem, {"_orm_adapt": True}) |
| 733 | for elem in util.to_list(self.parent_property.order_by) |
| 734 | ] |
| 735 | else: |
| 736 | self._order_by = None |
| 737 | |
| 738 | self.logger.info("%s lazy loading clause %s", self, self._lazywhere) |
| 739 | |
| 740 | # determine if our "lazywhere" clause is the same as the mapper's |
| 741 | # get() clause. then we can just use mapper.get() |
| 742 | # |
| 743 | # TODO: the "not self.uselist" can be taken out entirely; a m2o |
| 744 | # load that populates for a list (very unusual, but is possible with |
| 745 | # the API) can still set for "None" and the attribute system will |
| 746 | # populate as an empty list. |
| 747 | self.use_get = ( |
| 748 | not self.is_aliased_class |
| 749 | and not self.uselist |
| 750 | and self.entity._get_clause[0].compare( |
| 751 | self._lazywhere, |
| 752 | use_proxies=True, |
| 753 | compare_keys=False, |
| 754 | equivalents=self.mapper._equivalent_columns, |
| 755 | ) |
| 756 | ) |
| 757 | |
| 758 | if self.use_get: |
| 759 | for col in list(self._equated_columns): |
| 760 | if col in self.mapper._equivalent_columns: |
| 761 | for c in self.mapper._equivalent_columns[col]: |
| 762 | self._equated_columns[c] = self._equated_columns[col] |
| 763 | |
| 764 | self.logger.info( |
| 765 | "%s will use Session.get() to optimize instance loads", self |