|
| 1 | +""" |
| 2 | +Example demonstrating how to create lookup fields (n:1 relationships) between tables. |
| 3 | +
|
| 4 | +This example: |
| 5 | +1. Creates two tables: 'Project' and 'Task' |
| 6 | +2. Creates a lookup field in Task that references Project |
| 7 | +3. Creates a record in Project |
| 8 | +4. Creates a Task record linked to the Project |
| 9 | +5. Queries both records showing the relationship |
| 10 | +""" |
| 11 | + |
| 12 | +from dataverse_sdk import DataverseClient |
| 13 | +import os |
| 14 | +from datetime import datetime |
| 15 | + |
| 16 | +# Get credentials from environment variables |
| 17 | +BASE_URL = os.environ.get("DATAVERSE_URL") |
| 18 | + |
| 19 | +# Initialize client |
| 20 | +client = DataverseClient(BASE_URL) # Uses DefaultAzureCredential by default |
| 21 | + |
| 22 | +def main(): |
| 23 | + # 1. Create the Project table |
| 24 | + project_schema = { |
| 25 | + "name": "string", |
| 26 | + "description": "string", |
| 27 | + "start_date": "datetime", |
| 28 | + "end_date": "datetime", |
| 29 | + "budget": "decimal" |
| 30 | + } |
| 31 | + |
| 32 | + print("Creating Project table...") |
| 33 | + project_info = client.create_table("Project", project_schema) |
| 34 | + project_entity = project_info["entity_logical_name"] |
| 35 | + project_entity_set = project_info["entity_set_name"] |
| 36 | + print(f"Created Project table: {project_entity} (Set: {project_entity_set})") |
| 37 | + |
| 38 | + # 2. Create the Task table |
| 39 | + task_schema = { |
| 40 | + "title": "string", |
| 41 | + "description": "string", |
| 42 | + "status": "string", |
| 43 | + "due_date": "datetime", |
| 44 | + "estimated_hours": "decimal", |
| 45 | + } |
| 46 | + |
| 47 | + print("Creating Task table...") |
| 48 | + task_info = client.create_table("Task", task_schema) |
| 49 | + task_entity = task_info["entity_logical_name"] |
| 50 | + task_entity_set = task_info["entity_set_name"] |
| 51 | + print(f"Created Task table: {task_entity} (Set: {task_entity_set})") |
| 52 | + |
| 53 | + # 3. Create a lookup field from Task to Project |
| 54 | + print("Creating lookup relationship...") |
| 55 | + relationship_info = client.create_lookup_field( |
| 56 | + table_name=task_entity, |
| 57 | + field_name="project", |
| 58 | + target_table=project_entity, |
| 59 | + display_name="Project", |
| 60 | + description="The project this task belongs to", |
| 61 | + required_level="Recommended", # Recommended but not required |
| 62 | + cascade_delete="Cascade" # Delete tasks when project is deleted |
| 63 | + ) |
| 64 | + |
| 65 | + print(f"Created relationship: {relationship_info['relationship_name']}") |
| 66 | + print(f"Lookup field created: {relationship_info['lookup_field']}") |
| 67 | + |
| 68 | + # 4. Create a project record |
| 69 | + project_data = { |
| 70 | + "new_name": "Website Redesign", |
| 71 | + "new_description": "Complete overhaul of company website", |
| 72 | + "new_start_date": datetime.now().isoformat(), |
| 73 | + "new_end_date": datetime(2023, 12, 31).isoformat(), |
| 74 | + "new_budget": 25000.00 |
| 75 | + } |
| 76 | + |
| 77 | + print("Creating project record...") |
| 78 | + project_record = client.create(project_entity_set, project_data) |
| 79 | + project_id = project_record["new_projectid"] |
| 80 | + print(f"Created project with ID: {project_id}") |
| 81 | + |
| 82 | + # 5. Create a task linked to the project |
| 83 | + # The lookup field name follows the pattern: new_project_id |
| 84 | + lookup_field_name = relationship_info["lookup_field"].lower() + "id" |
| 85 | + |
| 86 | + task_data = { |
| 87 | + "new_title": "Design homepage mockup", |
| 88 | + "new_description": "Create initial design mockups for homepage", |
| 89 | + "new_status": "Not Started", |
| 90 | + "new_due_date": datetime(2023, 10, 15).isoformat(), |
| 91 | + "new_estimated_hours": 16.5, |
| 92 | + # Add the lookup reference |
| 93 | + lookup_field_name: project_id |
| 94 | + } |
| 95 | + |
| 96 | + print("Creating task record with project reference...") |
| 97 | + task_record = client.create(task_entity_set, task_data) |
| 98 | + task_id = task_record["new_taskid"] |
| 99 | + print(f"Created task with ID: {task_id}") |
| 100 | + |
| 101 | + # 6. Fetch the task with project reference |
| 102 | + print("Fetching task with project information...") |
| 103 | + expand_field = relationship_info["lookup_field"].lower() + "_expand" |
| 104 | + |
| 105 | + # Use the OData $expand syntax to retrieve the related project |
| 106 | + odata_client = client._get_odata() |
| 107 | + task_with_project = odata_client.get(task_entity_set, task_id, expand=[expand_field]) |
| 108 | + |
| 109 | + # Display the relationship information |
| 110 | + print("\nTask details:") |
| 111 | + print(f"Title: {task_with_project['new_title']}") |
| 112 | + print(f"Status: {task_with_project['new_status']}") |
| 113 | + |
| 114 | + project_ref = task_with_project.get(expand_field) |
| 115 | + if project_ref: |
| 116 | + print("\nLinked Project:") |
| 117 | + print(f"Name: {project_ref['new_name']}") |
| 118 | + print(f"Budget: ${project_ref['new_budget']}") |
| 119 | + |
| 120 | + print("\nRelationship successfully created and verified!") |
| 121 | + |
| 122 | +if __name__ == "__main__": |
| 123 | + main() |
0 commit comments