HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //usr/local/lib/python3.10/dist-packages/sqlalchemy/orm/__pycache__/relationships.cpython-310.pyc
o

���g���@s�dZddlmZddlZddlmZddlZddlZddlZddl	Z	ddl
Z
ddl
mZddl
mZddl
m
Z
ddl
mZdd	l
mZdd
l
mZddl
mZddl
mZdd
l
mZddl
mZddl
mZddl
mZddl
mZddl
mZddl
mZddl
mZddl
mZddl
mZddl
mZddlZddlm Z ddlm!Z!ddl"m#Z#ddl"m$Z$ddl%m&Z&ddl%m'Z'ddl%m(Z(dd l%m)Z)dd!l%m*Z*dd"l%m+Z+dd#l%m,Z,dd$l%m-Z-dd%l.m/Z/dd&l.m0Z0dd'l.m1Z1dd(l.m2Z2dd)l.m3Z3dd*l.m4Z4dd+l.m5Z5dd,l.m6Z6dd-l7m8Z8dd.l7m9Z9dd/l7m:Z:d0d1lm;Z<d0d2lm=Z=d0d3lm>Z>d0d4lm?Z?d0d5lm@Z@d0d6lm7Z7d0d7lAmZd0d8l@mBZBd0d9l@mCZCd0d:l@mDZDd0d;l@mEZEd0d<l@mFZFd0d=lGmHZHd0d>lGmIZId0d?lJmKZKd0d@lLmMZMd0dAlLmNZNd0dBlOmPZPd0dClOmQZQd0dDlOmRZRd0dElOmSZSd0dFlOmTZTd0dGlOmUZUd0dHlOmVZVd0dIlOmWZWd0dJlXmYZYd0dKlXmZZZd0dLlXm[Z[e
j\�r�ddMl"m]Z]ddNl"m^Z^ddOl"m_Z_ddPl"m`Z`ddQl"maZaddRl"mbZbddSl"mcZcddTl%mdZdddUlemfZfddVlemgZgddWlhmiZiddXljmkZkddYllmmZmddZlnmoZodd[lpmqZqdd\lrmsZsdd]ltmuZudd^l7mvZvdd_l7mwZwd0d`lGmxZxd0dalGmyZyd0dblGmzZzd0dclJm{Z{d0ddlJm|Z|d0delLm}Z}d0dflLm~Z~d0dglLmZd0dhl�m�Z�d0dil�m�Z�d0djlXm�Z�d0dklXm�Z�edledm�Z�ednedm�Z�edoedm�Z�edpedm�Z�edqedm�Z�ee�ee�egee�fdrdsegdrfegdsffZ�eZdtZ�ee�eHe�fZ�edue�egduffZ�eeZdve�eHeegeHefegeeHefeee�eHeffZ�ee�ee�ee�efffZ�eeMeeIeeEj�dwfZ�ee�ee�egee�fege�fe�fZ�edxeHedm�Z�edydzdm�Z�eeeNeeNefZ�eeeNeeNefZ�eeeNeeNefZ�d�d}d~�Z�d�dd��Z�ej�Gd�d��d�ee�e�f��Z�eeZdveeNed�ffZ�Gd�d��d�e�Z�e>j�Gd�d��d�e0e6e�e>j���Z�d�d�d��Z�Gd�d��d��Z�Gd�d��d��Z�e�d�d��Z�e�d~d��Z�Gd�d��d�e�e�e&e��Z�Gd�d��d�e�e�e-e�e)e��Z�dS)�a
Heuristics related to join conditions as used in
:func:`_orm.relationship`.

Provides the :class:`.JoinCondition` object, which encapsulates
SQL annotation and aliasing behavior focused on the `primaryjoin`
and `secondaryjoin` aspects of :func:`_orm.relationship`.

�)�annotationsN)�abc)�Any)�Callable)�cast)�
Collection)�Dict)�	FrozenSet)�Generic)�Iterable)�Iterator)�List)�
NamedTuple)�NoReturn)�Optional)�Sequence)�Set)�Tuple)�Type)�TypeVar)�Union�)�
attributes)�strategy_options)�insp_is_aliased_class)�is_has_collection_adapter)�_DeclarativeMapped)�_is_mapped_class)�class_mapper)�
DynamicMapped)�LoaderCallableStatus)�PassiveFlag)�	state_str)�WriteOnlyMapped)�_AttributeOptions)�_IntrospectsAnnotations)�
MANYTOMANY)�	MANYTOONE)�	ONETOMANY)�PropComparator)�RelationshipDirection)�StrategizedProperty)�
_orm_annotate)�_orm_deannotate)�CascadeOptions�)�exc)�Exists)�log)�schema)�sql)�util)�inspect)�	coercions)�
expression)�	operators)�roles)�visitors)�_ColumnExpressionArgument)�_HasClauseElement)�_safe_annotate)�ColumnClause)�
ColumnElement��_deep_annotate)�_deep_deannotate)�_shallow_annotate)�adapt_criterion_to_null)�
ClauseAdapter)�join_condition)�selectables_overlap��visit_binary_product)�de_optionalize_union_types)�Literal)�resolve_name_to_real_class_name)�_EntityType)�_ExternalEntityType)�_IdentityKeyType)�
_InstanceDict)�_InternalEntityType)�_O)�
_RegistryType)�Mapped)�_class_resolver)�_ModNS)�_ClassScanMapperConfig)�DependencyProcessor)�Mapper)�Query)�Session)�
InstanceState)�
LazyLoader)�AliasedClass)�AliasedInsp)�_CoreAdapterProto)�_EquivalentColumnMap)�	_InfoType)�_AnnotationDict)�SupportsAnnotations)�BinaryExpression)�
BindParameter)�
ClauseElement)�Table)�
FromClause)�_AnnotationScanType)�RODescriptorReference�_T)�bound�_T1�_T2�_PT�_PT2�
Mapper[_T]zAliasedClass[_T])
�select�joined�selectin�subquery�raise�raise_on_sql�noload�	immediate�
write_only�dynamicTFNrjFzMapped[Any]�_CEA�_CE�ColumnElement[Any]�expr�returncC�tt�tj|�ddi�S)aAnnotate a portion of a primaryjoin expression
    with a 'remote' annotation.

    See the section :ref:`relationship_custom_foreign` for a
    description of use.

    .. seealso::

        :ref:`relationship_custom_foreign`

        :func:`.foreign`

    �remoteT��_annotate_columnsr7�expectr:�ColumnArgumentRole�r��r��G/usr/local/lib/python3.10/dist-packages/sqlalchemy/orm/relationships.pyr��s�r�cCr�)aAnnotate a portion of a primaryjoin expression
    with a 'foreign' annotation.

    See the section :ref:`relationship_custom_foreign` for a
    description of use.

    .. seealso::

        :ref:`relationship_custom_foreign`

        :func:`.remote`

    �foreignTr�r�r�r�r�r��s�r�c@sBeZdZUdZdZded<ded<ded<ddd�Zddd�ZdS)�_RelationshipArgztstores a user-defined parameter value that must be resolved and
    parsed later at mapper configuration time.

    )�name�argument�resolved�strr�ror�z
Optional[_T2]r�r��boolcCs
|jduS�N)r���selfr�r�r��
_is_populateds
z_RelationshipArg._is_populated�clsregistry_resolver�&Callable[[str, bool], _class_resolver]�NonecCsN|j}t|t�r|||jdk��|_dSt|�r"t|�s"|�|_dS||_dS)N�	secondary)r��
isinstancer�r�r��callabler)r�r��
attr_valuer�r�r��_resolve_against_registrys

�
z*_RelationshipArg._resolve_against_registryN�r�r�)r�r�r�r�)�__name__�
__module__�__qualname__�__doc__�	__slots__�__annotations__r�r�r�r�r�r�r��s

r�.c@sBeZdZUdZded<ded<ded<ded<d	ed
<d	ed<dS)
�_RelationshipArgszWstores user-passed parameters that are resolved at mapper configuration
    time.

    zP_RelationshipArg[Optional[_RelationshipSecondaryArgument], Optional[FromClause]]r�z\_RelationshipArg[Optional[_RelationshipJoinConditionArgument], Optional[ColumnElement[Any]]]�primaryjoin�
secondaryjoinz>_RelationshipArg[_ORMOrderByArgument, _RelationshipOrderByArg]�order_byzN_RelationshipArg[Optional[_ORMColCollectionArgument], Set[ColumnElement[Any]]]�foreign_keys�remote_sideN)r�r�r�r�r�r�r�r�r�r�s
r�c$steZdZUdZejZdZ	dZdZ	de
d<de
d<edddddd�Zd	Z
d
e
d<de
d
<de
d<de
d<de
d<de
d<de
d<de
d<de
d<de
d<de
d<de
d<de
d<d e
d!<d"e
d#<				d�d	d	d	d	d	dd	d	dd$dd	d%ddddd	d	d	d	ddd	dd	d	d	d	d	ddd	dd&�"d��fd[d\�Zd�dadb�Zd�dedf�ZGdgdh�dhejee�Z			d�d�dndo�Z				d�d�dtdu�Z�dd|d}�Z			�d�ddd��Z�dd�d��Z�dd�d��Zejf�dd�d��Z		�d�dd�d��Ze �dd�d���Z!e"�d	d�d���Z#�d
d�d��Z$ej%�dd�d���Z&ej%�dd�d���Z'�d
�fd�d��Z(�d
d�d��Z)�d
d�d��Z*�dd�d„Z+e�,dá�d�dd�dƄ�Z-�d
d�dȄZ.e �dd�d˄�Z/e �dd�d΄�Z0ej%e�,dϡ�dd�d҄��Z1�d
d�dԄZ2e �dd�dׄ�Z3e3j4�dd�dׄ�Z3�dd�d܄Z5�dd�dބZ6�dd�d�Z7�dd�d�Z8�d
d�d�Z9e�,d��d
d�d��Z:ej%�dd�d��Z;ej%�dd�d��Z<									�d�dd�d��Z=�Z>S(�RelationshipPropertyz�Describes an object property that holds a single item or list
    of items that correspond to a related database table.

    Public constructor is the :func:`_orm.relationship` function.

    .. seealso::

        :ref:`relationship_config_toplevel`

    Tz
Sequence[str]�	_overlapsr^�_lazy_strategyF��passive_deletes�passive_updates�enable_typechecks�active_history�cascade_backrefsNzOptional[DependencyProcessor]�_dependency_processor�ColumnElement[bool]r��Optional[ColumnElement[bool]]r��Optional[FromClause]r��
JoinCondition�_join_condition�_RelationshipOrderByArgr��Set[ColumnElement[Any]]�_user_defined_foreign_keys�_calculated_foreign_keysr��
local_columns�_ColumnPairs�synchronize_pairs�Optional[_ColumnPairs]�secondary_synchronize_pairs�local_remote_pairsr*�	directionr��
_init_argszsave-update, mergert)"�uselist�collection_classr�r��back_populatesr��backref�overlaps�post_update�cascade�viewonly�attribute_options�lazyr�r�r�r�r�r��
join_depth�comparator_factory�
single_parent�	innerjoin�distinct_target_key�load_on_pending�query_class�info�	omit_join�sync_backref�doc�bake_queriesr��_local_remote_pairs�_legacy_inactive_history_styler��'Optional[_RelationshipArgumentType[_T]]�(Optional[_RelationshipSecondaryArgument]r��Optional[bool]r��EOptional[Union[Type[Collection[Any]], Callable[[], Collection[Any]]]]�,Optional[_RelationshipJoinConditionArgument]r��
Optional[str]�_ORMOrderByArgumentr��Optional[ORMBackrefArgument]r�r�r�r�r�r�r��Optional[_AttributeOptions]r��_LazyLoadArgumentTyper��Union[Literal['all'], bool]r�r�r�r��#Optional[_ORMColCollectionArgument]r��
Optional[int]r��4Optional[Type[RelationshipProperty.Comparator[Any]]]r�r�r�r�r��Optional[Type[Query[Any]]]r��Optional[_InfoType]r��Literal[None, False]r�r�r��
Literal[True]r��Literal[False]r�r�c"%
s�t�j|d�||_||_ttd|d�td|d�td|d�td|d�td|d�td|d��|_||_|
|_|
rA|j	|||||"d�|
rJ|rJt
�d	��||_||_
||_||_||_|"r`t
�d
��||_||_||_||_||_| |_||_|$|_||_|r�t�d�||_|#|_||_|p�tj |_!t�"|�|dur�|j#�$|�d|j
ff|_%t&�|_'|
r�t&t(�)d
|
��|_*nd|_*||_+||_,|j,r�|	r�t
�d��d|_-dS|	|_-dS)N)r�r�r�r�r�r�r�r�z-sync_backref and viewonly cannot both be TruezSThe 'cascade_backrefs' parameter passed to relationship() may only be set to False.z�setting omit_join to True is not supported; selectin loading of this relationship may not work correctly if this flag is set explicitly.  omit_join optimization is automatically detected for conditions under which it is supported.r�z\s*,\s*r�zCbackref and back_populates keyword arguments are mutually exclusive).�super�__init__r�r�r�r�r�r�r�� _warn_for_persistence_only_flags�sa_exc�
ArgumentErrorr�r�r�r�r�r�r�r�r�r�r�r�r�r�r5�warnr�r�r�r��
Comparatorr��set_creation_orderr��update�strategy_key�set�_reverse_property�re�splitr�r�r�r�)%r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r���	__class__r�r�r�ns�,





�	�����
�

zRelationshipProperty.__init__�kwrr�r�cKs4|��D]\}}||j|krt�d|f�qdS)Nz�Setting %s on relationship() while also setting viewonly=True does not make sense, as a viewonly=True relationship does not perform persistence operations. This configuration may raise an error in a future release.)�items�_persistence_onlyr5r�)r�r�k�vr�r�r�r��s����z5RelationshipProperty._warn_for_persistence_only_flags�mapper�Mapper[Any]cCs&tj|j|j|�||�||jd�dS)N)�
comparator�parententityr�)r�register_descriptor�class_�keyr�r��r�rr�r�r��instrument_class
s

�z%RelationshipProperty.instrument_classc@s�eZdZUdZdZded<ded<			dJdKdd�ZdLdd�Zded<	ded<	dMdd�ZdNdd�Z	dOd!d"�Z
dPd$d%�ZdQd)d*�ZdRd.d/�Z
dSd3d4�ZdZdTd5d6�Z	dUdVd;d<�Z	dUdWd=d>�Z	dUdWd?d@�ZdXdBdC�ZdTdDdE�ZdTdFdG�ZdYdHdI�ZdS)ZzRelationshipProperty.Comparatora�Produce boolean, comparison, and other operators for
        :class:`.RelationshipProperty` attributes.

        See the documentation for :class:`.PropComparator` for a brief
        overview of ORM level operator definition.

        .. seealso::

            :class:`.PropComparator`

            :class:`.ColumnProperty.Comparator`

            :class:`.ColumnOperators`

            :ref:`types_operators`

            :attr:`.TypeEngine.comparator_factory`

        )�entityr�property�_of_type�_extra_criteriaz0RODescriptorReference[RelationshipProperty[_PT]]�prop�Optional[_EntityType[_PT]]rNr��RelationshipProperty[_PT]�parentmapper�_InternalEntityType[Any]�adapt_to_entity�Optional[AliasedInsp[Any]]�of_type�extra_criteria�Tuple[ColumnElement[bool], ...]cCs.||_||_||_|r||_nd|_||_dS)z�Construction of :class:`.RelationshipProperty.Comparator`
            is internal to the ORM's attribute mechanics.

            N)r�
_parententity�_adapt_to_entityrr)r�rrrrrr�r�r�r�3s
z(RelationshipProperty.Comparator.__init__�AliasedInsp[Any]r��$RelationshipProperty.Comparator[Any]cCs|j|j|j||jd�S)N)rr)rrrr)r�rr�r�r�rHs�z/RelationshipProperty.Comparator.adapt_to_entity�_InternalEntityType[_PT]r�Mapper[_PT]rcCs|jrt|j�S|jjSr�)rr6rrr�r�r�r��_memoized_attr_entitygs
z5RelationshipProperty.Comparator._memoized_attr_entitycC�|jjSr��rrr�r�r�r��_memoized_attr_mappermsz5RelationshipProperty.Comparator._memoized_attr_mapperrjcCs|jr|jjS|jjjSr�)r�
selectabler�parent�_with_polymorphic_selectabler�r�r�r��_source_selectableps
z2RelationshipProperty.Comparator._source_selectabler�c	CsV|��}|jr
t|j�}nd}|jj|d|d|jd�\}}}}}}|dur)||@S|S)NT)�source_selectable�source_polymorphic�of_type_entity�alias_secondaryr)r+rr6r�
_create_joinsr)	r��
adapt_fromr.�pj�sj�source�destr��target_adapterr�r�r��__clause_element__vs*	��z2RelationshipProperty.Comparator.__clause_element__r�_EntityType[Any]�PropComparator[_PT]cCstj|j|j|j||jd�S)z�Redefine this object in terms of a polymorphic subclass.

            See :meth:`.PropComparator.of_type` for an example.


            �rrr)r�r�rrrr)r�rr�r�r�r�s�z'RelationshipProperty.Comparator.of_type�criteria�_ColumnExpressionArgument[bool]�PropComparator[Any]cGs:tdd�t�|�D��}tj|j|j|j|j|j	|d�S)z�Add AND criteria.

            See :meth:`.PropComparator.and_` for an example.

            .. versionadded:: 1.4

            css�|]
}t�tj|�VqdSr�)r7r�r:�WhereHavingRole)�.0�clauser�r�r��	<genexpr>�s
��
�z7RelationshipProperty.Comparator.and_.<locals>.<genexpr>r:)
�tupler5�coerce_generator_argr�r�rrrrr)r�r;�exprsr�r�r��and_�s
��z$RelationshipProperty.Comparator.and_�otherrrcCstd��)z�Produce an IN clause - this is not implemented
            for :func:`_orm.relationship`-based attributes at this time.

            zvin_() not yet supported for relationships.  For a simple many-to-one, use in_() against the set of foreign key values.)�NotImplementedError�r�rFr�r�r��in_�s�z#RelationshipProperty.Comparator.in_cCsl|dus
t|tj�r"|jjttfvr|��St|jj	d|j
d��S|jjr+t�
d��t|jj	||j
d��S)a�Implement the ``==`` operator.

            In a many-to-one context, such as:

            .. sourcecode:: text

              MyClass.some_prop == <some object>

            this will typically produce a
            clause such as:

            .. sourcecode:: text

              mytable.related_id == <some id>

            Where ``<some id>`` is the primary key of the given
            object.

            The ``==`` operator provides partial functionality for non-
            many-to-one comparisons:

            * Comparisons against collections are not supported.
              Use :meth:`~.Relationship.Comparator.contains`.
            * Compared to a scalar one-to-many, will produce a
              clause that compares the target columns in the parent to
              the given target.
            * Compared to a scalar many-to-many, an alias
              of the association table will be rendered as
              well, forming a natural join that is part of the
              main body of the query. This will not work for
              queries that go beyond simple AND conjunctions of
              comparisons, such as those which use OR. Use
              explicit joins, outerjoins, or
              :meth:`~.Relationship.Comparator.has` for
              more comprehensive non-many-to-one scalar
              membership tests.
            * Comparisons against ``None`` given in a one-to-many
              or many-to-many context produce a NOT EXISTS clause.

            N��adapt_source�]Can't compare a collection to an object or collection; use contains() to test for membership.)r�r8�Nullrr�r(r&�_criterion_existsr,�_optimized_compare�adapterr�r��InvalidRequestErrorrHr�r�r��__eq__�s")
�����z&RelationshipProperty.Comparator.__eq__�	criterion�)Optional[_ColumnExpressionArgument[bool]]�kwargsr1cKs�|durt�tj|�nd}t|dd�rFt|j�}|dusJ�|j|j|j	}}}|j
jr3|s3|��}|j
}|durE|durC||@}n|}nd}d}|jrR|��}	nd}	|j
j||	d�\}
}}}
}}|D]}t|j
jj|�||k}|dury|}qd||@}qd|dur�t|
�|@}nt|
|j
jd�}|dur�|r�|s�|�|�}|dur�|�ddi�}|tj�|�@}|dur�t�d��|��|
|��|
|�}|St�d��|��|
��|
�}|S)NrF)�dest_selectabler,)�exclude�no_replacement_traverseTr)r7r�r:r>�getattrr6rrr(�is_aliased_classr�_is_self_referential�_anonymous_fromclause�_single_table_criterionrPr+r0rr,r��traverse�	_annotater4�True_�_ifnone�exists�where�select_from�correlate_except)r�rSrU�where_criteriar��
target_mapper�
to_selectablerZ�single_critr,r2r3r4r5r�r6r�crit�j�exr�r�r�rNs����
�
�
	��
��
��
��z1RelationshipProperty.Comparator._criterion_existscKs$|jjs	t�d��|j|fi|��S)anProduce an expression that tests a collection against
            particular criterion, using EXISTS.

            An expression like::

                session.query(MyClass).filter(
                    MyClass.somereference.any(SomeRelated.x == 2)
                )

            Will produce a query like:

            .. sourcecode:: sql

                SELECT * FROM my_table WHERE
                EXISTS (SELECT 1 FROM related WHERE related.my_id=my_table.id
                AND related.x=2)

            Because :meth:`~.Relationship.Comparator.any` uses
            a correlated subquery, its performance is not nearly as
            good when compared against large target tables as that of
            using a join.

            :meth:`~.Relationship.Comparator.any` is particularly
            useful for testing for empty collections::

                session.query(MyClass).filter(~MyClass.somereference.any())

            will produce:

            .. sourcecode:: sql

                SELECT * FROM my_table WHERE
                NOT (EXISTS (SELECT 1 FROM related WHERE
                related.my_id=my_table.id))

            :meth:`~.Relationship.Comparator.any` is only
            valid for collections, i.e. a :func:`_orm.relationship`
            that has ``uselist=True``.  For scalar references,
            use :meth:`~.Relationship.Comparator.has`.

            z9'any()' not implemented for scalar attributes. Use has().�rr�r�rQrN�r�rSrUr�r�r��anyis
.�z#RelationshipProperty.Comparator.anycKs$|jjr	t�d��|j|fi|��S)a�Produce an expression that tests a scalar reference against
            particular criterion, using EXISTS.

            An expression like::

                session.query(MyClass).filter(
                    MyClass.somereference.has(SomeRelated.x == 2)
                )

            Will produce a query like:

            .. sourcecode:: sql

                SELECT * FROM my_table WHERE
                EXISTS (SELECT 1 FROM related WHERE
                related.id==my_table.related_id AND related.x=2)

            Because :meth:`~.Relationship.Comparator.has` uses
            a correlated subquery, its performance is not nearly as
            good when compared against large target tables as that of
            using a join.

            :meth:`~.Relationship.Comparator.has` is only
            valid for scalar references, i.e. a :func:`_orm.relationship`
            that has ``uselist=False``.  For collection references,
            use :meth:`~.Relationship.Comparator.any`.

            z3'has()' not implemented for collections. Use any().rmrnr�r�r��has�s
!�z#RelationshipProperty.Comparator.has�_ColumnExpressionArgument[Any]cKs@|jjs	t�d��|jj||jd�}|jjdur|�|�|_|S)a�	Return a simple expression that tests a collection for
            containment of a particular item.

            :meth:`~.Relationship.Comparator.contains` is
            only valid for a collection, i.e. a
            :func:`_orm.relationship` that implements
            one-to-many or many-to-many with ``uselist=True``.

            When used in a simple one-to-many context, an
            expression like::

                MyClass.contains(other)

            Produces a clause like:

            .. sourcecode:: sql

                mytable.id == <some id>

            Where ``<some id>`` is the value of the foreign key
            attribute on ``other`` which refers to the primary
            key of its parent object. From this it follows that
            :meth:`~.Relationship.Comparator.contains` is
            very useful when used with simple one-to-many
            operations.

            For many-to-many operations, the behavior of
            :meth:`~.Relationship.Comparator.contains`
            has more caveats. The association table will be
            rendered in the statement, producing an "implicit"
            join, that is, includes multiple tables in the FROM
            clause which are equated in the WHERE clause::

                query(MyClass).filter(MyClass.contains(other))

            Produces a query like:

            .. sourcecode:: sql

                SELECT * FROM my_table, my_association_table AS
                my_association_table_1 WHERE
                my_table.id = my_association_table_1.parent_id
                AND my_association_table_1.child_id = <some id>

            Where ``<some id>`` would be the primary key of
            ``other``. From the above, it is clear that
            :meth:`~.Relationship.Comparator.contains`
            will **not** work with many-to-many collections when
            used in queries that move beyond simple AND
            conjunctions, such as multiple
            :meth:`~.Relationship.Comparator.contains`
            expressions joined by OR. In such cases subqueries or
            explicit "outer joins" will need to be used instead.
            See :meth:`~.Relationship.Comparator.any` for
            a less-performant alternative using EXISTS, or refer
            to :meth:`_query.Query.outerjoin`
            as well as :ref:`orm_queryguide_joins`
            for more details on constructing outer joins.

            kwargs may be ignored by this operator but are required for API
            conformance.
            z9'contains' not implemented for scalar attributes.  Use ==rJN)	rr�r�rQrOrPr��'_Comparator__negated_contains_or_equals�negation_clause)r�rFrUr@r�r�r��contains�sA���z(RelationshipProperty.Comparator.containscs��jjtkr-t�|��d�fdd	��d�fdd
���jjr-tj���fdd��jj	D��Stjdd�t
�jjj�jj�
|��D��}��|�S)N�	local_colr��state�InstanceState[Any]�
remote_colr��BindParameter[Any]c
s.|j}tj|j|jd�j��jj|||�d�S)NT)�type_�unique�	callable_)�dictr4�	bindparamr
�typer�_get_attr_w_warn_on_noner)rurvrx�dict_r�r�r��state_bindparams��zURelationshipProperty.Comparator.__negated_contains_or_equals.<locals>.state_bindparam�colrcs�jr��|�S|Sr�)rP�r�r�r�r��adapt-s
zKRelationshipProperty.Comparator.__negated_contains_or_equals.<locals>.adaptc	s8g|]\}}t��|���|��|�k�|�dk��qSr�)r4�or_�r?�x�y)r�rvr�r�r��
<listcomp>5s��
��zPRelationshipProperty.Comparator.__negated_contains_or_equals.<locals>.<listcomp>cSsg|]\}}||k�qSr�r�r�r�r�r�r�@s��)rur�rvrwrxr�r�ry)r�rr�r)rr�r'r�instance_stater�_use_getr4rEr��zipr�primary_key�primary_key_from_instancerN)r�rFrSr�)r�r�rvr�r��__negated_contains_or_equalss&
�����
z<RelationshipProperty.Comparator.__negated_contains_or_equalscCs`|dus
t|tj�r |jjtkrt|jjd|jd��S|�	�S|jj
r)t�d��t|�
|��S)aSImplement the ``!=`` operator.

            In a many-to-one context, such as:

            .. sourcecode:: text

              MyClass.some_prop != <some object>

            This will typically produce a clause such as:

            .. sourcecode:: sql

              mytable.related_id != <some id>

            Where ``<some id>`` is the primary key of the
            given object.

            The ``!=`` operator provides partial functionality for non-
            many-to-one comparisons:

            * Comparisons against collections are not supported.
              Use
              :meth:`~.Relationship.Comparator.contains`
              in conjunction with :func:`_expression.not_`.
            * Compared to a scalar one-to-many, will produce a
              clause that compares the target columns in the parent to
              the given target.
            * Compared to a scalar many-to-many, an alias
              of the association table will be rendered as
              well, forming a natural join that is part of the
              main body of the query. This will not work for
              queries that go beyond simple AND conjunctions of
              comparisons, such as those which use OR. Use
              explicit joins, outerjoins, or
              :meth:`~.Relationship.Comparator.has` in
              conjunction with :func:`_expression.not_` for
              more comprehensive non-many-to-one scalar
              membership tests.
            * Comparisons against ``None`` given in a one-to-many
              or many-to-many context produce an EXISTS clause.

            NrJrL)r�r8rMrr�r'r,rOrPrNr�r�rQrrrHr�r�r��__ne__Ks+���z&RelationshipProperty.Comparator.__ne__cCs|jj��|jSr�)rr)�_check_configurer�r�r�r��_memoized_attr_property�sz7RelationshipProperty.Comparator._memoized_attr_property)NNr�)
rrrrrrrrrr)rr r�r!)r�r")r�r#)r�rj�r�r�)rr8r�r9)r;r<r�r=)rFrr�r)rFrr�r�r�)rSrTrUrr�r1)rSrTrUrr�r�)rFrqrUrr�r�)r�r)r�r�r�r�r�r�r�rr$r'r+r7rrErI�__hash__rRrNrorprtrrr�r�r�r�r�r�r�sB
�









@�h�8�
'
R
3>r��instance�objectr/�from_entity�Optional[_EntityType[Any]]cCsN|dusJ�d}|durt|�}|dusJ�t|�r|jj}|j|d||d�S)NT)�value_is_parentrKr/)r6r�_adapter�adapt_clauserO)r�r�r/r�rK�inspr�r�r��_with_parent�s�z!RelationshipProperty._with_parentrvr�rK�Optional[_CoreAdapterProto]cs�dur(zt���Wntjyd�Ynw�dus!t�dd�s(t�d���|}�dur6�j||d�S|sB�jj�jj}�n	�jj	�jj
}�|rQ�j�n�j�t
������d�����fd	d
�}�jdurw|rwt�j����|�}t�|id|i�}|r�||�}|S)N�is_instanceFz�Mapped instance expected for relationship comparison to object.   Classes, queries and other SQL elements are not accepted in this context; for comparison with a subquery, use %s.has(**criteria).rJr~ryr�r�cs*|j�vr������|j�|_dSdSr�)�_identifying_keyr�r�)r~��bind_to_colr�rr�rvr�r��visit_bindparam�s

��z@RelationshipProperty._optimized_compare.<locals>.visit_bindparam)r~ryr�r�)r6r��NoInspectionAvailablerYr��_lazy_none_clauser��
_lazywhere�_bind_to_col�_rev_lazywhere�_rev_bind_to_colrr)r�
instance_dict�objr�rFr\r^r;�cloned_traverse)r�rvr�rKr/�reverse_directionrSr�r�r�r�rO�sR������	��
�z'RelationshipProperty._optimized_comparerwr�rQ�columnr��Callable[[], Any]cs8��������j��j�d������fdd�}|S)aKCreate the callable that is used in a many-to-one expression.

        E.g.::

            u1 = s.query(User).get(5)

            expr = Address.user == u1

        Above, the SQL should be "address.user_id = 5". The callable
        returned by this method produces the value "5" based on the identity
        of ``u1``.

        r�rcs��dusJ���j}}|tju}�j����jrtjntjtjAd�}|tj	ur8|s7t
�d�t��f��n|tj
urK|sJt
�d�t��f��n|}|durXt�d��|S)N��passivezUCan't resolve value for column %s on object %s; no value has been set for this columnz`Can't resolve value for column %s on object %s; the object is detached and the value was expiredz�Got None for value of column %s; this is unsupported for a relationship comparison and will not currently produce an IS comparison (but may in a future release))r
r �NO_VALUE�_get_state_attr_by_column�
persistentr!�PASSIVE_OFF�PASSIVE_NO_FETCH�INIT_OK�	NEVER_SETr�rQr"�PASSIVE_NO_RESULTr5r�)�
last_known�	to_return�existing_is_available�
current_value�r�r��	lkv_fixedrrrvr�r��_gosH��
�

���

�����z:RelationshipProperty._get_attr_w_warn_on_none.<locals>._goN)r�r)�get_property_by_column�_track_last_known_valuer
�_last_known_values)r�rrvr�r�r�r�r�r�r��s

1/z-RelationshipProperty._get_attr_w_warn_on_noner�cCsD|s|jj|jj}}n	|jj|jj}}t||�}|r ||�}|Sr�)r�r�r�r�r�rE)r�r�rKrSr�r�r�r�r�Ps��
z&RelationshipProperty._lazy_none_clausecCst|jjj�d|jS)N�.)r�r)rr�r
r�r�r�r��__str__fszRelationshipProperty.__str__�sessionr\�source_state�source_dict�
dest_state�	dest_dict�load�
_recursive�Dict[Any, object]�_resolve_conflict_map�#Dict[_IdentityKeyType[Any], object]c	Cs�|r|jD]}	||	f|vrdSqd|jvrdS|j|vrdS|jr�|�|j�}
t|
�s.J�|
�||�}|
jr<|jr=J�	|rL|�|j�j	||t
jd�g}|D]%}
t�
|
�}t�|
�}d|||f<|j|||||d�}|duru|�|�qP|s�t�|||j�}|D]}|�|�q�dS|�|j�}t|�s�J�|j|||dt
jd�dS||j}
|
dur�t�
|
�}t�|
�}d|||f<|j|||||d�}nd}|s�|||j<dS|�|j��|||d�dS)N�mergeTr�)r�r�r�F)�_adaptr�)r��_cascader
r��get_implr�get_collection�
collection�empty�getr!�
PASSIVE_MERGErr�r��_merge�append�init_state_collection�append_without_eventr�)r�r�r�r�r�r�r�r�r��r�impl�instances_iterable�	dest_list�current�
current_state�current_dictr��coll�c�	dest_implr�r�r�r�is�
�

�

�
���
�


��zRelationshipProperty.merge�InstanceState[_O]r
r�r!�&Sequence[Tuple[InstanceState[_O], _O]]cCsf|j|j}|j|||d�}|tjus|durgSt|�r+dd�|j||||d�D�St�|�|fgS)z�Return a list of tuples (state, obj) for the given
        key.

        returns an empty list if the value is None/empty/PASSIVE_NO_RESULT
        r�NcSsg|]	}t�|�|f�qSr�)rr�)r?�or�r�r�r��s��z;RelationshipProperty._value_as_iterable.<locals>.<listcomp>)	�managerr�r�r r�rr�rr�)r�rvr�r
r�r�r�r�r�r��_value_as_iterable�s
�z'RelationshipProperty._value_as_iterablerz�visited_states�Set[InstanceState[Any]]�halt_on�.Optional[Callable[[InstanceState[Any]], bool]]�DIterator[Tuple[Any, Mapper[Any], InstanceState[Any], _InstanceDict]]c
cs
�|dks|jrtj}ntjtjB}|dkr"|j|jj�||�}n
|j	|||j|d�}|dko4d|j
v}|D]K\}	}
|	|vr@q7|
durEq7|	dusKJ�t�|
�}|rW||	�rWq7|r]|	js]q7|	jj
}|�|j
jj
�svtd|j|jj|
jf��|�|	�|
||	|fVq7dS)N�deletezsave-updater�zrefresh-expire�
delete-orphanz@Attribute '%s' on class '%s' doesn't handle objects of type '%s')r�r!�PASSIVE_NO_INITIALIZEr��NO_RAISEr�r
r��get_all_pendingr�r�rr�r�isa�
class_manager�AssertionErrorr)rr�add)
r�rzrvr�r�r�r��tuples�skip_pendingr�r�r��instance_mapperr�r�r��cascade_iterator�s@�
��

��
�z%RelationshipProperty.cascade_iteratorcCs|jrdS|jduS)NF)r�r�r�r�r�r��_effective_sync_backref s
z,RelationshipProperty._effective_sync_backref�rel_a�RelationshipProperty[Any]�rel_bcCsJ|jr|jrt�d||f��|jr|js!|jdur#d|_dSdSdSdS)NzQRelationship %s cannot specify sync_backref=True since %s includes viewonly=True.F)r�r�r�rQ)rrr�r�r��_check_sync_backref's����

�z(RelationshipProperty._check_sync_backrefcCs�|jj|dd�}t|t�st�d||f��|�||�|�||�|j�|�|j�|�|�	�|j�
|j�sEt�d||||jf��|j
r`|jttfvrb|j|jkrdt�d|||jf��dSdSdS)NF)�_configure_mappersz�back_populates on relationship '%s' refers to attribute '%s' that is not a relationship.  The back_populates parameter should refer to the name of a relationship on the target class.zereverse_property %r on relationship %s references relationship %s, which does not reference mapper %szv%s and back-reference %s are both of the same direction %r.  Did you mean to set remote_side on the many-to-one side ?)r�get_propertyr�r�r�rQrr�r��
_setup_entity�
common_parentr)r��_configure_startedr�r(r')r�r
rFr�r�r��_add_reverse_property7s<
��
���
���z*RelationshipProperty._add_reverse_property�_InternalEntityType[_T]cCs|j��|jS)z�Return the target mapped entity, which is an inspect() of the
        class or aliased class that is referenced by this
        :class:`.RelationshipProperty`.

        )r)r�rr�r�r�r�rcs
zRelationshipProperty.entityrscCr%)zcReturn the targeted :class:`_orm.Mapper` for this
        :class:`.RelationshipProperty`.

        r&r�r�r�r�rmszRelationshipProperty.mappercsn|��|��|��|��|��|�|j�|��|��|j	�
�t���t
d|�d��|_dS)Nr^))r�rt)�_check_conflicts�_process_dependent_argumentsr�_setup_registry_dependencies�_setup_join_conditions�_check_cascade_settingsr��
_post_init�_generate_backrefr��"_warn_for_conflicting_sync_targetsr��do_initr�
_get_strategyr�r�rr�r�rus



�zRelationshipProperty.do_initcCs|jjj�|jjj�dSr�)r)r�registry�_set_depends_onrr�r�r�r�r
�s
�z1RelationshipProperty._setup_registry_dependenciescCs|j}dD]}t||�}|�|jd�qdD]}t||�}|j}|dur1ttjtj	||d��|_q|j
j}|durGt|�rGt�
d||f��|jjdurc|jjdurctdd	�t�|jj�D��|_nd|_t�d
d	�t�|jj�D��|_t�dd	�t�|jj�D��|_dS)z�Convert incoming configuration arguments to their
        proper form.

        Callables are resolved, ORM annotations removed.

        )r�r�r�r�r�r�r)r�r�N��argnamez�secondary argument %s passed to to relationship() %s must be a Table object or other FROM clause; can't send a mapped class directly as rows in 'secondary' are persisted independently of a class that is mapped to that same table.Fcs�"�|]}tjtj|dd�VqdS)r�rN�r7r�r:r��r?r�r�r�r�rA�����
�zDRelationshipProperty._process_dependent_arguments.<locals>.<genexpr>csr)r�rNrrr�r�r�rA�rcsr)r�rNrrr�r�r�rA�r)r�rYr��_clsregistry_resolversr�r-r7r�r:r�r�rr�r�r�rBr5�to_list�
column_set�
to_column_setr�r�r�)r��	init_args�attr�rel_arg�valr�r�r�r�r�sB

������

�
�z1RelationshipProperty._process_dependent_arguments�	decl_scanrXrrT�cls�	Type[Any]�originating_module�mapped_container�Optional[Type[Mapped[Any]]]�
annotation�Optional[_AnnotationScanType]�extracted_mapped_annotation�is_dataclass_fieldc
Cs�|}
|dur|jdur|�||�ndS|}
|dusJ�|durDt|t�}t|t�}|r7d|_d|jff|_n|rCd|_d|jff|_nd}}t|
�}
t|
d�r�|
j	}
t
|
t�rvt|
tj
�rv|jdurut�|
�rrt�d|
�d���|
|_n|s}|s}d|_|
jr�t
|
t�r�t|
tj�r�|
jd}n|
jd	}t|d
�r�|j}t||�}
n|}
nt�d|
�d���t|
d
�r�|
j}
t|
|�}
|jdur�|s�|s�d|_|jdur�td
|
�|_dSdS)Nr|r�r}F�
__origin__zCollection annotation type z� cannot be instantiated; please provide an explicit 'collection_class' parameter (e.g. list, set, etc.) to the relationship() function to accompany this annotation���r�__forward_arg__zGeneric alias z requires an argumentz_RelationshipArgumentType[_T])r��_raise_for_required�
issubclassr#rr�r�rK�hasattrr/r�rrrr��_py_inspect�
isabstractr�r�r��__args__�typing�Mappingr1rMr)r�r%rr&r(r
r)r+r-r.r��
is_write_only�
is_dynamic�
arg_origin�type_arg�str_argumentr�r�r��declarative_scan�s|


�
�


�	��

�
�
�
��
�z%RelationshipProperty.declarative_scanzsqlalchemy.orm.mapper�_RelationshipProperty__argumentcCs�d|jvrdStjj}|r|}n|j}t|t�r"td|�|���}nt	|�r2t|t
|jf�s2|�}n|}t|t
�r@t|dd�}n$zt
|�}WntjyRd}Ynwt|d�sdt�d|jt
|�f��||_|jj|_dS)Nrz_ExternalEntityType[Any]F��	configurerzErelationship '%s' expects a class or a mapper argument (received: %s))�__dict__r5�	preloaded�
orm_mapperr�r�r�r�_clsregistry_resolve_namer�rrZrr6r�r�r4r�r
r�persist_selectable�target)r�r@�	mapperlibr��resolved_argumentrr�r�r�r9s>


�

�
�
��z"RelationshipProperty._setup_entitycCs�tdid|jj�d|jj�d|jj�d|jj�d|jjj�d|jjj�d|jj	j�d|jj
�d	|jj
�d
|j�d|j
�d|j�d
|j�d|�d|j�d|j��|_}|j|_|j	|_	|j|_|j|_|j
|_
|j|_|j|_|j|_|j|_|j|_dS)N�parent_persist_selectable�child_persist_selectable�parent_local_selectable�child_local_selectabler�r�r��parent_equivalents�child_equivalents�consider_as_foreign_keysr�r��self_referentialr�support_sync�can_be_synced_fnr�)r�r)rGr�local_tabler�r�r�r�r��_equivalent_columnsrr�r�r�r[r��_columns_are_mappedr�r��remote_columnsr�r��foreign_key_columnsr�r�)r��jcr�r�r�risV����
�
�
��	�
���
���
�z+RelationshipProperty._setup_join_conditionsr�cC�
|jdS)Nr�rr�r�r�r��_clsregistry_resolve_arg��
z-RelationshipProperty._clsregistry_resolve_arg�>Callable[[str], Callable[[], Union[Type[Any], Table, _ModNS]]]cCr[)Nrr\r�r�r�r�rF�r^z.RelationshipProperty._clsregistry_resolve_namezsqlalchemy.orm.clsregistry�mTuple[Callable[[str], Callable[[], Union[Type[Any], Table, _ModNS]]], Callable[[str, bool], _class_resolver]]cCstjjj}||jj|�Sr�)r5rD�orm_clsregistry�	_resolverr)r)r�rbr�r�r�r�s
z+RelationshipProperty._clsregistry_resolverscCsJ|jjr!t|jjdd��|j�s#t�d|j|jjj|jjjf��dSdS)zOTest that this relationship is legal, warn about
        inheritance conflicts.FrAz�Attempting to assign a new relationship '%s' to a non-primary mapper on class '%s'.  New relationships can only be added to the primary mapper, i.e. the very first mapper created for class '%s' N)	r)�non_primaryrr�has_propertyr
r�r�r�r�r�r�r�r�s
������z%RelationshipProperty._check_conflictsr.cCs|jS)z\Return the current cascade setting for this
        :class:`.RelationshipProperty`.
        )r�r�r�r�r�r��szRelationshipProperty.cascade�Union[str, CascadeOptions]cCs|�|�dSr�)�_set_cascade�r�r�r�r�r�r����cascade_argcCsNt|�}|jrt|�tj��}d|jvr|�|�||_|jr%||j_dSdS)Nr)	r.r��intersection�_viewonly_cascadesrCrr�r�r�)r�rir�r�r�r�rf�s
�

�z!RelationshipProperty._set_cascadecCs�|jr+|js+|jtus|jtur+tjd||jturdnd|jjj	|j
jj	d�dd��|jdkr?d|vs8d	|vr?t�d
|��|jrR|j
��j
�|j|jjf�dSdS)Na�For %(direction)s relationship %(rel)s, delete-orphan cascade is normally configured only on the "one" side of a one-to-many relationship, and not on the "many" side of a many-to-one or many-to-many relationship.  To force this relationship to allow a particular "%(relatedcls)s" object to be referenced by only a single "%(clsname)s" object at a time via the %(rel)s relationship, which would allow delete-orphan cascade to take place in this direction, set the single_parent=True flag.zmany-to-onezmany-to-many)�relr��clsname�
relatedcls�bbf0��code�allr�r�z^On %s, can't set passive_deletes='all' in conjunction with 'delete' or 'delete-orphan' cascade)�
delete_orphanr�r�r&r'r�r�r)rr�rr��primary_mapper�_delete_orphansr�r
rgr�r�r�r�s:��
����
����z,RelationshipProperty._check_cascade_settingscCs|j|jvo
|j|j|uS)zaReturn True if this property will persist values on behalf
        of the given mapper.

        )r
�
relationshipsrr�r�r��
_persists_for�s�z"RelationshipProperty._persists_for�colscGsR|jjj}|D]}|dur|j�|�rq|jjj�|�s&|jj�|�s&dSqdS)z�Return True if all columns in the given collection are
        mapped by the tables referenced by this :class:`.RelationshipProperty`.

        NFT)r�r�r�r��contains_columnr)rGrH)r�rxr�r�r�r�r�rWs

���z(RelationshipProperty._columns_are_mappedcCs�|jjrdS|jdur�|js�t|jt�r|ji}}n|j\}}|j��}|jsIt	|�
���|j�}|D]}|�
|�rH|jsHt�d|||f��q4|jdur_|�d|jj�}|�d|jj�}n|�d|jj�}|�dd�}|rtt�d��|�d|j�}|j��}	|�d|j�|�d|j�|�d	|j�|�d
|j�||_t|	|jf||||jd�|��}
|j||
dd
�|jr�|� |j�dSdS)zlInterpret the 'backref' instruction to create a
        :func:`_orm.relationship` complementary to this one.Nz]Error creating backref '%s' on relationship '%s': property of that name exists on mapper '%s'r�r�zOCan't assign 'secondaryjoin' on a backref against a non-secondary relationship.r�r�r�r�r�)r�r�r�r�T)�warn_for_existing)!r)rcr�r�r�r�rrt�concreter��iterate_to_root�union�self_and_descendantsrdr�r�r��popr��secondaryjoin_minus_local�primaryjoin_minus_local�primaryjoin_reverse_remoterQr��
setdefaultr�r�r�r�r�r
�_configure_propertyr	)r��backref_keyrUr�check�mr2r3r�r)�relationshipr�r�r�rs~

����
�����
���	��z&RelationshipProperty._generate_backrefzsqlalchemy.orm.dependencycCs:tjj}|jdur|jtu|_|js|j�|�|_	dSdSr�)
r5rD�orm_dependencyr�r�r'r�rY�from_relationshipr�)r��
dependencyr�r�r�res
���zRelationshipProperty._post_initcCs|j}|jS)zPmemoize the 'use_get' attribute of this RelationshipLoader's
        lazyloader.)r��use_get)r��strategyr�r�r�r�pszRelationshipProperty._use_getcCs|j�|j�Sr�)rrr)r�r�r�r�r[xrhz)RelationshipProperty._is_self_referentialr�r-r,rVr.�"Optional[_InternalEntityType[Any]]rr��Tuple[ColumnElement[bool], Optional[ColumnElement[bool]], FromClause, FromClause, Optional[FromClause], Optional[ClauseAdapter]]cCsd}|r|jdurd}|dur|r|jjr|jj}|r(|j}|dur'|j}d}n|j}|durG|jj}|jjr9d}|jrF|durF|��}d}n||jjusQ|jjrSd}|j	}	|pd|duod||jjupd|j
}|j�||||	|�\}
}}}
}|dur||jj
}|dur�|jj
}|
|||||
fS)NFT)r�r)�with_polymorphicr*rr(rr[r\r]�_is_subqueryr��join_targetsrU)r�r-r,rVr.r/r�aliased�dest_mapperrir�r�r�r6r�r�r�r0|sr��������z"RelationshipProperty._create_joins)NN)Hr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�)rrr�r�)rrr�r�)TN)r�r�r/r�r�r�r�r�)FNT)
rvrr�r�rKr�r/r�r�r�)
rrrvrwr�rQr�r�r�r�)FN)r�r�rKr�r�r��r�r�)r�r\r�rwr�rQr�rwr�rQr�r�r�r�r�r�r�r�)
rvr�r�rQr
r�r�r!r�r�r�)rzr�rvrwr�rQr�r�r�r�r�r�r�)rrrrr�r�)r
r�r�r�)r�r
)r�rs�r�r�)r%rXrrTr&r'r(r�r
r�r)r*r+r,r-r,r.r�r�r�)r@rr�r�)r�r�)r�r_)r�r`)r�r.)r�rer�r�)rirer�r�)r�r.r�r�)rrr�r�)rxr�r�r�)FNNNFr�)r-r�r,r�rVr�r.r�r/r�rrr�r�)?r�r�r�r�r�_RELATIONSHIP_TOKEN�strategy_wildcard_key�
inherit_cache�_links_to_entity�_is_relationshipr�r}rr�r�r�rr5�
MemoizedSlotsr)rqr�r�rOr�r�r�r�r!r�r�r�rr��staticmethodrr	�memoized_propertyrrrr
rr?�preload_modulerrr]rFrrr��setterrfrrwrWrrr�r[r0�
__classcell__r�r�rr�r�5s
���

	��Dm�f��=,	Ld/
.P
�r��elementrrdcs(d��fdd��|dur�|�}d�|S)N�elemrr�cs*t|tj�r
|�����}|j�d�|S)N)�clone)r�r8r?r_�copy�_copy_internals)r��rr�r�r�r��sz _annotate_columns.<locals>.clone)r�rr�rr�)r�rr�r�r�r��s
r�c@sZeZdZUded<ded<ded<ded<ded	<d
ed<d
ed<d
ed<ded<ded<ded<ded<ded<dddddddddddd�d�d�d(d)�Zd�d,d-�Zd�d.d/�Zd�d0d1�Zed�d2d3��Z	ed�d4d5��Z
ejd�d6d7��Z
d�d<d=�Zejd�d>d?��Zejd�d@dA��Zd�dBdC�Zd�dDdE�Zd�dFdG�Zd�dHdI�Zd�dJdK�Zd�dLdM�Zd�dNdO�Zd�dSdT�Zd�dUdV�Zd�dWdX�Zd�dYdZ�Zd�d[d\�Zd�d]d^�Zd�d_d`�Zd�dadb�Zd�dedf�Z d�dgdh�Z!d�dldm�Z"d�dndo�Z#e$�%�Z&dpedq<d�drds�Z'ejd�dudv��Z(ejd�dwdx��Z)ejd�dydz��Z*d�d{d|�Z+d�dd��Z,ejd�d�d���Z-		�d�d�d�d��Z.d�d�d�d��Z/dS)�r�r��primaryjoin_initialr�r�r�r�r�rrr�r�r�r*r�rjrKrLrMrNr�r�NFTcG�dS�NTr�)r�r�r�r��<lambda>	szJoinCondition.<lambda>)r�r�r�rOrPrQr�r�rRrSrTrO�Optional[_EquivalentColumnMap]rPrQrr�r�rRrSr�rT�Callable[..., bool]cCs�||_||_||_||_||_|	|_||_||_||_|
|_	||_
||_||_|
|_
||_||_|��|jdus;J�|��|��|��|��|��|��|�|jd�|jdurf|�|jd�|��|��|��dS�NTF)rKrMrLrNrOrPr�r�r�rQr��_remote_siderrRrSrT�_determine_joinsr��_sanitize_joins�
_annotate_fks�_annotate_remote�_annotate_local�_annotate_parentmapper�_setup_pairs�_check_foreign_cols�_determine_direction�_check_remote_side�
_log_joins)r�rKrLrMrNr�r�r�rOrPrQr�r�rRrrSrTr�r�r�r��s<
zJoinCondition.__init__r�r�cCs�|jj}|�d|j|j�|�d|j|j�|�d|jd�dd�|jD���|�d|jd�dd�|jp5gD���|�d	|jd�d
d�|jD���|�d|jd�dd�|j	D���|�d
|jd�dd�|j
D���|�d|j|j�dS)Nz%s setup primary join %sz%s setup secondary join %sz%s synchronize pairs [%s]�,cs� �|]\}}d||fVqdS�z
(%s => %s)Nr��r?�lr�r�r�r�rA+	��
�z+JoinCondition._log_joins.<locals>.<genexpr>z#%s secondary synchronize pairs [%s]csr�r�r�r�r�r�r�rA2	s
�
�
�z%s local/remote pairs [%s]csr�)z	(%s / %s)Nr�r�r�r�r�rA:	r�z%s remote columns [%s]cs��|]}d|VqdS�z%sNr��r?r�r�r�r�rAA	��z%s local columns [%s]csr�r�r�r�r�r�r�rAF	r�z%s relationship direction %s)r�loggerr�r�r��joinr�r�r�rXr�r�)r�r2r�r�r�r�$	sF
��
��
����zJoinCondition._log_joinscCs2t|jdd�|_|jdurt|jdd�|_dSdS)a_remove the parententity annotation from our join conditions which
        can leak in here based on some declarative patterns and maybe others.

        "parentmapper" is relied upon both by the ORM evaluator as well as
        the use case in _join_fixture_inh_selfref_w_entity
        that relies upon it being present, see :ticket:`3364`.

        )r
�	proxy_key��valuesN)rCr�r�r�r�r�r�r�J	s
�
��zJoinCondition._sanitize_joinsc
Csb|jdur|jdurt�d|j��zQ|jpd}|jdurI|jdur.t|j|j|j|d�|_|j	durBt|j
|j|j|d�|_WdS|j	|_WdS|j	dur]t|j
|j|j|d�|_WdS|j	|_WdStj
y�}z|jdur}t�
d|j|jf�|�t�
d|j�|�d}~wtjy�}z|jdur�t�d|j|jf�|�t�d|j�|�d}~ww)z�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.

        NzMProperty %s specified with secondary join condition but no secondary argument)�a_subsetrQa1Could not determine join condition between parent/child tables on relationship %s - there are no foreign keys linking these tables via secondary table '%s'.  Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify 'primaryjoin' and 'secondaryjoin' expressions.aCould not determine join condition between parent/child tables on relationship %s - there are no foreign keys linking these tables.  Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify a 'primaryjoin' expression.alCould not determine join condition between parent/child tables on relationship %s - there are multiple foreign key paths linking the tables via secondary table '%s'.  Specify the 'foreign_keys' argument, providing a list of those columns which should be counted as containing a foreign key reference from the secondary table to each of the parent and child tables.a'Could not determine join condition between parent/child tables on relationship %s - 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.)r�r�r�r�rrQrGrLrNr�rKrMr��NoForeignKeysError�AmbiguousForeignKeysError)r�rQ�nfe�afer�r�r�r�\	s���


�
�
�

��	�����	

��
������zJoinCondition._determine_joinscCst|jdd�S�N��localr�r�)rCr�r�r�r�r�r��	rhz%JoinCondition.primaryjoin_minus_localcCs|jdusJ�t|jdd�Sr�)r�rCr�r�r�r�r��	sz'JoinCondition.secondaryjoin_minus_localcCs>|jrddd�}t�|ji|�S|jrt|jd	d
�St|j�S)
a(Return the primaryjoin condition suitable for the
        "reverse" direction.

        If the primaryjoin was delivered here with pre-existing
        "remote" annotations, the local/remote annotations
        are reversed.  Otherwise, the local/remote annotations
        are removed.

        r�rrrr��
Optional[_CE]c[s\d|jvrt|j�}|d=d|d<|�|�Sd|jvr,t|j�}|d=d|d<|�|�SdS)Nr�Tr�)�_annotationsr}�_with_annotations)r�rrr�r�r��replace�	s





z9JoinCondition.primaryjoin_reverse_remote.<locals>.replacer�r�N�r�rrrr�r�)�_has_remote_annotationsr;�replacement_traverser��_has_foreign_annotationsrC)r�r�r�r�r�r��	s
�
z(JoinCondition.primaryjoin_reverse_remoter@rhr+r�cCs&t�|i�D]
}||jvrdSqdSr�)r;�iterater�)r�r@r+r�r�r�r��_has_annotation�	s

�zJoinCondition._has_annotationcC�|�|jd�S�Nr��r�r�r�r�r�r�r��	rhz&JoinCondition._has_foreign_annotationscCr��Nr�r�r�r�r�r�r��	rhz%JoinCondition._has_remote_annotationscCs(|jrdS|jr|��dS|��dS)z�Annotate the primaryjoin and secondaryjoin
        structures with 'foreign' annotations marking columns
        considered as foreign.

        N)r�rQ�_annotate_from_fk_list�_annotate_present_fksr�r�r�r�r��	s
zJoinCondition._annotate_fkscsDd	�fdd�}t��ji|��_�jdur t��ji|��_dSdS)
Nr�rrrr�r�cs|�jvr|�ddi�SdS�Nr�T)rQr_�r�rr�r�r��check_fk
s
z6JoinCondition._annotate_from_fk_list.<locals>.check_fkr�)r;r�r�r�)r�r�r�r�r�r�
s�

��z$JoinCondition._annotate_from_fk_listcsz|jdur
t�|jj��nt��d
�fdd��d�fdd�}t�|jid|i�|_|jdur;t�|jid|i�|_dSdS)N�ar��br��Optional[ColumnElement[Any]]csdt|tj�rt|tj�r|�|�r|S|�|�r|S�r0|�vr&|�vr&|S|�vr0|�vr0|SdSr�)r�r3�Column�
references)r�r�)�
secondarycolsr�r��
is_foreign
s

z7JoinCondition._annotate_present_fks.<locals>.is_foreign�binary�BinaryExpression[Any]r�cs�t|jtj�rt|jtj�sdSd|jjvrId|jjvrK�|j|j�}|durM|�|j�r8|j�ddi�|_dS|�|j�rO|j�ddi�|_dSdSdSdSdSr�)r��leftr4r@�rightr��comparer_)r�r�)r�r�r��visit_binary+
s(��
��z9JoinCondition._annotate_present_fks.<locals>.visit_binary)r�r�r�r�r�r��r�r�r�r�)	r�r5rr�r�r;r�r�r��r�r�r�)r�r�r�r�
s
�

��z#JoinCondition._annotate_present_fkscs:|j�|j�d�d	���fdd�}t�|jid|i��S)
zvReturn True if the join condition contains column
        comparisons where both columns are in both tables.

        Fr�r�r�r�csv|j|j}}t|tj�r/t|tj�r1��|j�r3��|j�r5��|j�r7��|j�r9d�dSdSdSdSdSdSdSr�)r�r�r�r8r?�is_derived_from�table)r�r��f��mt�pt�resultr�r�r�O
s
�
�
�
�
�
��z;JoinCondition._refers_to_parent_table.<locals>.visit_binaryNr�)rKrLr;r^r�r�r�r�r��_refers_to_parent_tableF
s
z%JoinCondition._refers_to_parent_tablecCst|j|j�S)z5Return True if parent/child tables have some overlap.)rHrKrLr�r�r�r��_tables_overlap_
s�zJoinCondition._tables_overlapcCst|jrdS|jdur|��dS|js|jr|��dS|��r*|�dd�d�dS|��r4|�	�dS|�
�dS)z�Annotate the primaryjoin and secondaryjoin
        structures with 'remote' annotations marking columns
        considered as part of the 'remote' side.

        NcSs
d|jvSr��r�r�r�r�r�r�u
s
z0JoinCondition._annotate_remote.<locals>.<lambda>F)r�r��_annotate_remote_secondaryr�r��_annotate_remote_from_argsr��_annotate_selfrefr��_annotate_remote_with_overlap�%_annotate_remote_distinct_selectablesr�r�r�r�r�f
s
�zJoinCondition._annotate_remotecsX|jdusJ�|j�d
�fdd	�}t�|ji|�|_|jdus!J�t�|ji|�|_dS)z^annotate 'remote' in primaryjoin, secondaryjoin
        when 'secondary' is present.

        Nr�rrrr�r�cs�j�|�r
|�ddi�SdS�Nr�T)r�ryr_r���fixed_secondaryr�r��repl�
sz6JoinCondition._annotate_remote_secondary.<locals>.replr�)r�r;r�r�r��r�rr�rr�r�|
s�
�z(JoinCondition._annotate_remote_secondary�fn�$Callable[[ColumnElement[Any]], bool]�remote_side_givencs,d���fdd�}t��jid|i��_dS)	zxannotate 'remote' in primaryjoin, secondaryjoin
        when the relationship is detected as self-referential.

        r�r�r�r�cs�|j�|j�}t|jtj�r9t|jtj�r9�|j�r#|j�ddi�|_�|j�r5|s7|j�ddi�|_dSdSdS�sA���dSdSr)r�r�r�r�r8r?r_�_warn_non_column_elements)r��equated�rr
r�r�r�r��
s�
��z5JoinCondition._annotate_selfref.<locals>.visit_binaryNr�)r;r�r�)r�rr
r�r�r
r�r�
s

�zJoinCondition._annotate_selfrefcsr|jr|jrt�d��dd�|jD��n|j�|��r'|��fdd�d�dSd�fd
d�}t�|ji|�|_dS)z�annotate 'remote' in primaryjoin, secondaryjoin
        when the 'remote_side' or '_local_remote_pairs'
        arguments are used.

        zTremote_side argument is redundant against more detailed _local_remote_side argument.cSsg|]\}}|�qSr�r�r�r�r�r�r��
�z<JoinCondition._annotate_remote_from_args.<locals>.<listcomp>cs|�vSr�r�r��r�r�r�r��
sz:JoinCondition._annotate_remote_from_args.<locals>.<lambda>Tr�rrrr�r�cs|t��vr
|�ddi�SdSr)r�r_r�rr�r�r�
sz6JoinCondition._annotate_remote_from_args.<locals>.replNr�)	r�r�r�r�r�rr;r�r�rr�rr�r��
s�
�z(JoinCondition._annotate_remote_from_argscsRd�fdd�}�jduo�jj�jju�d��fdd
��t��jid|i��_dS)z�annotate 'remote' in primaryjoin, secondaryjoin
        when the parent/child tables have some set of
        tables in common, though is not a fully self-referential
        relationship.

        r�r�r�r�cs0�|j|j�\|_|_�|j|j�\|_|_dSr��r�r�)r�)�proc_left_rightr�r�r��
s��zAJoinCondition._annotate_remote_with_overlap.<locals>.visit_binaryNr�r�r��-Tuple[ColumnElement[Any], ColumnElement[Any]]cs�t|tj�r%t|tj�r%�jj�|�r!�jj�|�r!|�ddi�}||fS�r<|j�	d��j
jur<|�ddi�}||fS�rS|j�	d��j
jurS|�ddi�}||fS���||fS)Nr�Tr)
r�r8r?rLr�ryrKr_r�r�rrrr)�check_entitiesr�r�r�r�
s,�����	���zDJoinCondition._annotate_remote_with_overlap.<locals>.proc_left_rightr�)r�r�r�r�r�r)rrr)r;r�r�r�r�)rrr�r�r�
s	�
�z+JoinCondition._annotate_remote_with_overlapcs$d
�fdd�}t��ji|��_d	S)z}annotate 'remote' in primaryjoin, secondaryjoin
        when the parent/child tables are entirely
        separate.

        r�rrrr�r�cs<�jj�|�r�jj�|�r�jj�|�r|�ddi�SdSr)rLr�ryrMrNr_r�r�r�r�rs��zAJoinCondition._annotate_remote_distinct_selectables.<locals>.replNr��r;r�r�rr�r�r�r�
s
�z3JoinCondition._annotate_remote_distinct_selectablescCst�d|j�dS)Nz�Non-simple column elements in primary join condition for property %s - consider using remote() annotations to mark the remote side.)r5r�rr�r�r�r�rs
��z'JoinCondition._warn_non_column_elementscsb|�|jd�r	dS|jrt�dd�|jD���nt�|jj��d
�fdd�}t�|ji|�|_dS)aCAnnotate the primaryjoin and secondaryjoin
        structures with 'local' annotations.

        This annotates all column elements found
        simultaneously in the parent table
        and the join condition that don't have a
        'remote' annotation set up from
        _annotate_remote() or user-defined.

        r�NcSsg|]\}}|�qSr�r�r�r�r�r�r�(rz1JoinCondition._annotate_local.<locals>.<listcomp>r�rrrr�r�cs$d|jvr|�vr|�ddi�SdS)Nr�r�T)r�r_r���
local_sider�r��locals_-sz.JoinCondition._annotate_local.<locals>.locals_r�)	r�r�r�r5rrKr�r;r�)r�rr�rr�r�s�
�zJoinCondition._annotate_localcs$d	�fdd�}t��ji|��_dS)
Nr�rrrr�r�cs<d|jvr|�d�jji�Sd|jvr|�d�jji�SdS)Nr�rr�)r�r_rrr)r�r�r�r��parentmappers_7s


z<JoinCondition._annotate_parentmapper.<locals>.parentmappers_r�r)r�rr�r�r�r�6s
�z$JoinCondition._annotate_parentmappercCs\|jst�d|jf��t�|jj��|j	j�}|jD]\}}||vr+t�
d|f�qdS)NaRelationship %s could not determine any unambiguous local/remote column pairs based on join condition and remote_side arguments.  Consider using the remote() annotation to accurately mark those elements of the join condition that are on the remote side of the relationship.z�Expression %s is marked as 'remote', but these column(s) are local to the local side.  The remote() annotation is needed only for a self-referential relationship where both sides of the relationship refer to the same tables.)r�r�r�rr5rrKr��
differencerLr�)r��
not_target�_�rmtr�r�r�r�Bs(���
�����z JoinCondition._check_remote_siderG�primarycCs�d}|�|d�}t|�}|rt|j�}nt|j�}|jr|s#|js%|r%dS|jrA|rA|sAd|r1dp2d||jf}|d7}t�|��d|rFdpGd||jf}|d	7}t�|��)
zHCheck the foreign key columns collected and emit error
        messages.Fr�Nz�Could not locate any simple equality expressions involving locally mapped foreign key columns for %s join condition '%s' on relationship %s.rr�a  Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or are annotated in the join condition with the foreign() annotation. To allow comparison operators other than '==', the relationship can be marked as viewonly=True.z`Could not locate any relevant foreign key columns for %s join condition '%s' on relationship %s.z�  Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or are annotated in the join condition with the foreign() annotation.)�_gather_columns_with_annotationr�r�r�rSrr�r�)r�rGr�can_sync�foreign_cols�has_foreign�errr�r�r�r�^sP�
����
����

���	�
z!JoinCondition._check_foreign_colscCs|jdur
t|_dSt�|jj�}t�|jj�}|�|j	�}|�|j	�}|rk|rk|�
|jdd�}dd�|�
|jd�D�}|rQ|rQ|j�|j
�}|�|�}|�|�}|rZ|sZt|_dS|rc|sct|_dSt�d|j��|rrt|_dS|ryt|_dSt�d|j��)z[Determine if this relationship is one to many, many to one,
        many to many.

        Nr�r�cSsh|]	}d|jvr|�qS)r�r��r?r�r�r�r��	<setcomp>�s

�z5JoinCondition._determine_direction.<locals>.<setcomp>aDCan't determine relationship direction for relationship '%s' - foreign key columns within the join condition are present in both the parent and the child's mapped tables.  Ensure that only those columns referring to a parent column are marked as foreign, either via the foreign() annotation or via the foreign_keys argument.z�Can't determine relationship direction for relationship '%s' - foreign key columns are present in neither the parent nor the child's mapped tables)r�r&r�r5rrKr�rLrjrYrr�rXr�rr(r'r�r�r)r��
parentcols�
targetcols�onetomany_fk�manytoone_fk�onetomany_local�manytoone_local�self_equatedr�r�r�r��sN

	����



��


��z"JoinCondition._determine_directionr��_ColumnPairIterable�_MutableColumnPairscCsdd�|D�S)z�provide deannotation for the various lists of
        pairs, so that using them in hashes doesn't incur
        high-overhead __eq__() comparisons against
        original columns mapped.

        cSs g|]\}}|��|��f�qSr���_deannotater�r�r�r�r��s z3JoinCondition._deannotate_pairs.<locals>.<listcomp>r�)r�r�r�r�r��_deannotate_pairs�s	zJoinCondition._deannotate_pairscs|g}t�g��g}d	��fdd�}�j|f�j|ffD]\}}|dur$q|||�q�����_��|��_��|��_dS)
N�joincondr�r�r-r�r�cs d
���fdd	�}t||�dS)Nr�r�r�r�r�r�r�cs�d|jvrd|jvr��|�r��||f�nd|jvr-d|jvr-��|�r-��||f�|jtjurU��||�rWd|jvrG��||f�dSd|jvrY��||f�dSdSdSdS)Nr�r�)r�rTr��operatorr9�eqr�)r�r�r�)r��lrpr�r�r�r�
s&

�

��

�z<JoinCondition._setup_pairs.<locals>.go.<locals>.visit_binary)r�r�r�r�r�r�r�r�rI)r1r�r��r4r�)r�r��gosz&JoinCondition._setup_pairs.<locals>.go)r1r�r�r-r�r�)r5�
OrderedSetr�r�r0r�r�r�)r��
sync_pairs�secondary_sync_pairsr6r1r�r�r5r�r��s � �
�zJoinCondition._setup_pairszwweakref.WeakKeyDictionary[ColumnElement[Any], weakref.WeakKeyDictionary[RelationshipProperty[Any], ColumnElement[Any]]]�_track_overlapping_sync_targetscs�|jsdSdd�|jD�dd�|jD�D]�\}��|jvr*t�|j|i�|j�<qg}|j�}|��D]]\}}|jj	s�||jj
vr�|j|jjvr�|jj|jvr�d|jjvr�d|jvr�|jj
�|j
�s�|jj�|j�s�|jj
�|j�s�|jj�|j
�s�|jj|jks�|jj
�|j
�s�|�||f�q5|r�tjd|j|�d�t�fdd�|D���d	�td
d�|D���|jfdd�||j�|j<qdS)
NcS�g|]\}}||f�qSr�r��r?�from_�to_r�r�r�r�C��zDJoinCondition._warn_for_conflicting_sync_targets.<locals>.<listcomp>cSr;r�r�r<r�r�r�r�Er?z__*arelationship '%s' will copy column %s to column %s, which conflicts with relationship(s): %s. If this is not the intention, consider if these relationships should be linked with back_populates, or if viewonly=True should be applied to one or more if they are read-only. For the less common case that foreign key constraints are partially overlapping, the orm.foreign() annotation can be used to isolate the columns that should be written towards.   To silence this warning, add the parameter 'overlaps="%s"' to the '%s' relationship.z, c3s"�|]\}}d||�fVqdS)z'%s' (copies %s to %s)Nr�)r?�pr�fr_�r>r�r�rA�s
��
�zCJoinCondition._warn_for_conflicting_sync_targets.<locals>.<genexpr>r�css�|]\}}|jVqdSr��r
)r?r@�frr�r�r�rA�s��qzyxrp)rSr�r�r:�weakref�WeakKeyDictionaryrrr�_dispose_calledr�r
r�r)�
is_siblingrr�r5r�r��sorted)r�r=�other_props�prop_to_fromr@rAr�rBr�r:sl���

�
�
������������z0JoinCondition._warn_for_conflicting_sync_targetsr�cC�
|�d�Sr���_gather_join_annotationsr�r�r�r�rX��
zJoinCondition.remote_columnscCrM)Nr�rNr�r�r�r�r��rPzJoinCondition.local_columnscCrMr�rNr�r�r�r�rY�rPz!JoinCondition.foreign_key_columnscCs>t|�|j|��}|jdur|�|�|j|��dd�|D�S)NcSsh|]}|���qSr�r.rr�r�r�r$�rz9JoinCondition._gather_join_annotations.<locals>.<setcomp>)r�rr�r�r�)r�r+�sr�r�r�rO�s�
��z&JoinCondition._gather_join_annotationsr��
Iterable[str]cs"t|���fdd�t�|i�D�S)Ncs&h|]}��|j�rttt|��qSr�)�issubsetr�rr@rr���annotation_setr�r�r$�s
��z@JoinCondition._gather_columns_with_annotation.<locals>.<setcomp>)r�r;r�)r�r@r+r�rTr�r�s

�z-JoinCondition._gather_columns_with_annotation�FrozenSet[ColumnElement[Any]]cCs,|jdurttjdd�|jjD���StjS)NcSsg|]}|j�qSr�)�	proxy_setr#r�r�r�r��sz8JoinCondition._secondary_lineage_set.<locals>.<listcomp>)r��	frozenset�	itertools�chainr�r5�	EMPTY_SETr�r�r�r��_secondary_lineage_set�s

�z$JoinCondition._secondary_lineage_setr�r,rVr�rirr�tTuple[ColumnElement[bool], Optional[ColumnElement[bool]], Optional[FromClause], Optional[ClauseAdapter], FromClause]csZt|ddi�}�j�j�j}}}|dur#|dur||@}n||@}|rJd�fd	d
��t�fdd�|D��}|durC|tj|�@}n|tj|�@}|r�|dur~|jdd
�}t|t	d�}	t|�j
d��|	�}
|durxt|t	d��t|�jd��}	|
�
|�}nt|t	�j
d�}	|dur�|	�t|t�jd��d}
|	�
|�}|
p�|	}d|_nd}|||||fS)a7Given a source and destination selectable, create a
        join between them.

        This takes into account aliasing the join clause
        to reference the appropriate corresponding columns
        in the target objects, as well as the extra child
        criterion, equivalent column sets, etc.

        rXTNr�rerrdr�cs>|j�dd�}|�jjur|�jjur|�jvrt||�S|S)anote unrelated columns in the "extra criteria" as either
                should be adapted or not adapted, even though they are not
                part of our "local" or "remote" side.

                see #9779 for this case, as well as #11010 for a follow up

                rN)r�r�rr)rr\r>)r�r�parentmapper_for_elementr�r�r��mark_exclude_cols�s�

z5JoinCondition.join_targets.<locals>.mark_exclude_colsc3s"�|]}t|ddi�d�VqdS)�should_not_adaptT)�annotate_callableNrA)r?r�)r_r�r�rA
s���
�z-JoinCondition.join_targets.<locals>.<genexpr>)�flat)�
exclude_fn)�equivalents)rcrd)r�rerrdr�re)rDr�r�r�rBr4rEr\rF�_local_col_excluderPrZrOr^�_remote_col_excluderc)r�r,rVr�rirr�r�r��primary_aliasizer�secondary_aliasizerr6r�)r_r�r�r��s��
�
�	�������
���
�zJoinCondition.join_targetsr��gTuple[ColumnElement[bool], Dict[str, ColumnElement[Any]], Dict[ColumnElement[Any], ColumnElement[Any]]]c	si�i}|jdu��r&t�t��|jD]\}}�|�||f�|||<qn�s5|jD]\}}|||<q+n|jD]\}}|||<q8d����fdd�}|j}|jdusU�s\t�|i|�}|jdurs|j}�rmt�|i|�}t	�
||�}�fd	d
��D�}|||fS)Nr�r�rrr��Optional[BindParameter[Any]]csX�sd|jvs�r*�r|�vs�s*d|jvr*|�vr&tjdd|jdd��|<�|SdS)Nr�r�T)rzr{)r�r4r~rr���binds�
has_secondary�lookupr�r�r��col_to_bind^
s"�
���


�z5JoinCondition.create_lazy_clause.<locals>.col_to_bindcsi|]}�|j|�qSr�rCr�)rlr�r��
<dictcomp>~
sz4JoinCondition.create_lazy_clause.<locals>.<dictcomp>)r�r�rrr�rj)r��collections�defaultdict�listr�r�r�r;r�r4rE)	r�r��equated_columnsr�r�ro�	lazywherer�r�r�rkr��create_lazy_clauseH
s<


�
�
�
�
z JoinCondition.create_lazy_clause) rKrjrLrjrMrjrNrjr�r�r�r�r�r�rOr�rPr�rQrr�r�r�rrRrrrrSr�rTr�r�r�)r@rhr+r�r�r�r�)rr	r
r�r�r�)rGr�rr�r�r�)r�r,r�r-)r�r�)r+r�r�r�)r@r�r+rRr�r�)r�rV)Nr�)r,r�rVrjr�r�rir�rrr�r])F)r�r�r�ri)0r�r�r�r�r�r�r�r�rr�r�r5r�r�r�r�r�r�r�r�r�r�r�r�rr�rrrr�r�r�r�r�r0r�rFrGr:rrXr�rYrOrr\r�rvr�r�r�r�r��s�
	�
5
&
_
$



2





!
1





D
R
9
�
V



�r�c@s(eZdZdZdZddd�Zddd�Zd
S)�_ColInAnnotationsz�Serializable object that tests for names in c._annotations.

    TODO: does this need to be serializable anymore?  can we find what the
    use case was for that?

    )�namesrxr�cGst|�|_dSr�)rXrx)r�rxr�r�r�r��
sz_ColInAnnotations.__init__r�rhr�r�cCst|j�|j��Sr�)r�rxrjr�)r�r�r�r�r��__call__�
sz_ColInAnnotations.__call__N)rxr�)r�rhr�r�)r�r�r�r�r�r�ryr�r�r�r�rw�
s

rwr�r`c@seZdZdZdZdS)�Relationshipa�Describes an object property that holds a single item or list
    of items that correspond to a related database table.

    Public constructor is the :func:`_orm.relationship` function.

    .. seealso::

        :ref:`relationship_config_toplevel`

    .. versionchanged:: 2.0 Added :class:`_orm.Relationship` as a Declarative
       compatible subclass for :class:`_orm.RelationshipProperty`.

    TN)r�r�r�r�r�r�r�r�r�rz�
srzc@s$eZdZdZdZ	eddd��ZdS)	�_RelationshipDeclaredz>Relationship subclass used implicitly for declarative mapping.Tr�r�cCr�)Nrzr�)r&r�r�r��_mapper_property_name�
sz+_RelationshipDeclared._mapper_property_nameNr�)r�r�r�r�r��classmethodr|r�r�r�r�r{�
sr{)r�r~r�r~)r�rrrdr�r)�r��
__future__rrqr�dataclassesr6r5rYr�r8rrrrrr	r
rrr
rrrrrrrrrrF�rr�_typingrr�baserrrrr r!r"r#�
interfacesr$r%r&r'r(r)r*r+r5r,r-r.r0r�r1r2r3r4�
inspectionr7r8r9r:r;�sql._typingr<r=�sql.annotationr>�sql.elementsr?r@�sql.utilrBrCrDrErFrGrHrJ�util.typingrKrLrM�
TYPE_CHECKINGrNrOrPrQrRrSrTrU�clsregistryrVrW�	decl_baserXr�rYrrZ�queryr[r�r\rvr]�
strategiesr^r_r`rarbrcrdrerfrgrh�
sql.schemari�sql.selectablerjrkrlrmrorprqrrr��_RelationshipArgumentTyper�r��"_RelationshipJoinConditionArgument�_RelationshipSecondaryArgumentr��ORMBackrefArgument�
DMLColumnRole�_ORMColCollectionElement�_ORMColCollectionArgumentr~rr,r�r-r�r��	dataclassr�r�r��class_logger�
Identifiedr�r�r�rwrerfrzr{r�r�r�r��<module>s�

��
�
������
��	


�
)
/


�
�