Search multiple entities for a key, raise if ambiguous or not found. This is used by filter_by() to search across all FROM clause entities when a single entity doesn't have the requested attribute. .. versionadded:: 2.1 Raises: AmbiguousColumnError: If key exists in multip
(
entities: Collection[Any],
key: str,
)
| 2652 | |
| 2653 | |
| 2654 | def _entity_namespace_key_search_all( |
| 2655 | entities: Collection[Any], |
| 2656 | key: str, |
| 2657 | ) -> SQLCoreOperations[Any]: |
| 2658 | """Search multiple entities for a key, raise if ambiguous or not found. |
| 2659 | |
| 2660 | This is used by filter_by() to search across all FROM clause entities |
| 2661 | when a single entity doesn't have the requested attribute. |
| 2662 | |
| 2663 | .. versionadded:: 2.1 |
| 2664 | |
| 2665 | Raises: |
| 2666 | AmbiguousColumnError: If key exists in multiple entities |
| 2667 | InvalidRequestError: If key doesn't exist in any entity |
| 2668 | """ |
| 2669 | |
| 2670 | match_: SQLCoreOperations[Any] | None = None |
| 2671 | |
| 2672 | for entity in entities: |
| 2673 | ns = _entity_namespace(entity) |
| 2674 | # Check if the attribute exists |
| 2675 | if hasattr(ns, key): |
| 2676 | if match_ is not None: |
| 2677 | entity_desc = ", ".join(str(e) for e in list(entities)[:3]) |
| 2678 | if len(entities) > 3: |
| 2679 | entity_desc += f", ... ({len(entities)} total)" |
| 2680 | raise exc.AmbiguousColumnError( |
| 2681 | f'Attribute name "{key}" is ambiguous; it exists in ' |
| 2682 | f"multiple FROM clause entities ({entity_desc}). " |
| 2683 | f"Use filter() with explicit column references instead " |
| 2684 | f"of filter_by()." |
| 2685 | ) |
| 2686 | match_ = getattr(ns, key) |
| 2687 | |
| 2688 | if match_ is None: |
| 2689 | # No entity has this attribute |
| 2690 | entity_desc = ", ".join(str(e) for e in list(entities)[:3]) |
| 2691 | if len(entities) > 3: |
| 2692 | entity_desc += f", ... ({len(entities)} total)" |
| 2693 | raise exc.InvalidRequestError( |
| 2694 | f'None of the FROM clause entities have a property "{key}". ' |
| 2695 | f"Searched entities: {entity_desc}" |
| 2696 | ) |
| 2697 | |
| 2698 | return match_ |
no test coverage detected