|
11 | 11 | from .core._auth import _AuthManager |
12 | 12 | from .core.config import DataverseConfig |
13 | 13 | from .data._odata import _ODataClient |
| 14 | +from .models.metadata import ( |
| 15 | + LookupAttributeMetadata, |
| 16 | + OneToManyRelationshipMetadata, |
| 17 | + ManyToManyRelationshipMetadata, |
| 18 | + Label, |
| 19 | + LocalizedLabel, |
| 20 | + CascadeConfiguration, |
| 21 | +) |
14 | 22 |
|
15 | 23 |
|
16 | 24 | class DataverseClient: |
@@ -701,16 +709,15 @@ def flush_cache(self, kind) -> int: |
701 | 709 | # Relationship operations |
702 | 710 | def create_one_to_many_relationship( |
703 | 711 | self, |
704 | | - lookup: Any, |
705 | | - relationship: Any, |
| 712 | + lookup: LookupAttributeMetadata, |
| 713 | + relationship: OneToManyRelationshipMetadata, |
706 | 714 | solution_unique_name: Optional[str] = None, |
707 | 715 | ) -> Dict[str, Any]: |
708 | 716 | """ |
709 | 717 | Create a one-to-many relationship between tables. |
710 | 718 |
|
711 | 719 | This operation creates both the relationship and the lookup attribute |
712 | | - on the referencing table. It mirrors the CreateOneToManyRequest from |
713 | | - the .NET SDK. |
| 720 | + on the referencing table. |
714 | 721 |
|
715 | 722 | :param lookup: Metadata defining the lookup attribute. |
716 | 723 | :type lookup: ~PowerPlatform.Dataverse.models.metadata.LookupAttributeMetadata |
@@ -768,15 +775,14 @@ def create_one_to_many_relationship( |
768 | 775 |
|
769 | 776 | def create_many_to_many_relationship( |
770 | 777 | self, |
771 | | - relationship: Any, |
| 778 | + relationship: ManyToManyRelationshipMetadata, |
772 | 779 | solution_unique_name: Optional[str] = None, |
773 | 780 | ) -> Dict[str, Any]: |
774 | 781 | """ |
775 | 782 | Create a many-to-many relationship between tables. |
776 | 783 |
|
777 | 784 | This operation creates a many-to-many relationship and an intersect table |
778 | | - to manage the relationship. It mirrors the CreateManyToManyRequest from |
779 | | - the .NET SDK. |
| 785 | + to manage the relationship. |
780 | 786 |
|
781 | 787 | :param relationship: Metadata defining the many-to-many relationship. |
782 | 788 | :type relationship: ~PowerPlatform.Dataverse.models.metadata.ManyToManyRelationshipMetadata |
@@ -854,5 +860,92 @@ def get_relationship(self, schema_name: str) -> Optional[Dict[str, Any]]: |
854 | 860 | with self._scoped_odata() as od: |
855 | 861 | return od._get_relationship(schema_name) |
856 | 862 |
|
| 863 | + def create_lookup_field( |
| 864 | + self, |
| 865 | + referencing_table: str, |
| 866 | + lookup_field_name: str, |
| 867 | + referenced_table: str, |
| 868 | + display_name: Optional[str] = None, |
| 869 | + description: Optional[str] = None, |
| 870 | + required: bool = False, |
| 871 | + cascade_delete: str = "RemoveLink", |
| 872 | + solution_unique_name: Optional[str] = None, |
| 873 | + language_code: int = 1033, |
| 874 | + ) -> Dict[str, Any]: |
| 875 | + """ |
| 876 | + Create a simple lookup field relationship. |
| 877 | +
|
| 878 | + This is a convenience method that wraps :meth:`create_one_to_many_relationship` |
| 879 | + for the common case of adding a lookup field to an existing table. |
| 880 | +
|
| 881 | + :param referencing_table: Logical name of the table that will have the lookup field (child table). |
| 882 | + :type referencing_table: :class:`str` |
| 883 | + :param lookup_field_name: Schema name for the lookup field (e.g., ``"new_AccountId"``). |
| 884 | + :type lookup_field_name: :class:`str` |
| 885 | + :param referenced_table: Logical name of the table being referenced (parent table). |
| 886 | + :type referenced_table: :class:`str` |
| 887 | + :param display_name: Display name for the lookup field. Defaults to the referenced table name. |
| 888 | + :type display_name: :class:`str` or None |
| 889 | + :param description: Optional description for the lookup field. |
| 890 | + :type description: :class:`str` or None |
| 891 | + :param required: Whether the lookup is required. Defaults to ``False``. |
| 892 | + :type required: :class:`bool` |
| 893 | + :param cascade_delete: Delete behavior (``"RemoveLink"``, ``"Cascade"``, ``"Restrict"``). |
| 894 | + Defaults to ``"RemoveLink"``. |
| 895 | + :type cascade_delete: :class:`str` |
| 896 | + :param solution_unique_name: Optional solution to add the relationship to. |
| 897 | + :type solution_unique_name: :class:`str` or None |
| 898 | + :param language_code: Language code for labels. Defaults to 1033 (English). |
| 899 | + :type language_code: :class:`int` |
| 900 | +
|
| 901 | + :return: Dictionary with ``relationship_id``, ``lookup_schema_name``, and related metadata. |
| 902 | + :rtype: :class:`dict` |
| 903 | +
|
| 904 | + :raises ~PowerPlatform.Dataverse.core.errors.HttpError: If the Web API request fails. |
| 905 | +
|
| 906 | + Example: |
| 907 | + Create a simple lookup field:: |
| 908 | +
|
| 909 | + result = client.create_lookup_field( |
| 910 | + referencing_table="new_order", |
| 911 | + lookup_field_name="new_AccountId", |
| 912 | + referenced_table="account", |
| 913 | + display_name="Account", |
| 914 | + required=True, |
| 915 | + cascade_delete="RemoveLink" |
| 916 | + ) |
| 917 | +
|
| 918 | + print(f"Created lookup: {result['lookup_schema_name']}") |
| 919 | + """ |
| 920 | + # Build the label |
| 921 | + localized_labels = [LocalizedLabel(label=display_name or referenced_table, language_code=language_code)] |
| 922 | + |
| 923 | + # Build the lookup attribute |
| 924 | + lookup = LookupAttributeMetadata( |
| 925 | + schema_name=lookup_field_name, |
| 926 | + display_name=Label(localized_labels=localized_labels), |
| 927 | + required_level="ApplicationRequired" if required else "None", |
| 928 | + ) |
| 929 | + |
| 930 | + # Add description if provided |
| 931 | + if description: |
| 932 | + lookup.description = Label( |
| 933 | + localized_labels=[LocalizedLabel(label=description, language_code=language_code)] |
| 934 | + ) |
| 935 | + |
| 936 | + # Generate a relationship name |
| 937 | + relationship_name = f"{referenced_table}_{referencing_table}_{lookup_field_name}" |
| 938 | + |
| 939 | + # Build the relationship metadata |
| 940 | + relationship = OneToManyRelationshipMetadata( |
| 941 | + schema_name=relationship_name, |
| 942 | + referenced_entity=referenced_table, |
| 943 | + referencing_entity=referencing_table, |
| 944 | + referenced_attribute=f"{referenced_table}id", |
| 945 | + cascade_configuration=CascadeConfiguration(delete=cascade_delete), |
| 946 | + ) |
| 947 | + |
| 948 | + return self.create_one_to_many_relationship(lookup, relationship, solution_unique_name) |
| 949 | + |
857 | 950 |
|
858 | 951 | __all__ = ["DataverseClient"] |
0 commit comments