Microsoft Dataverse中的表关系定义表行如何与其他表或同一表中的行相关联。 有两种类型的表关系:一对多和多对多。 可以使用关系 API 在表之间创建关系,如以下部分所示。
from PowerPlatform.Dataverse.models import (
CascadeConfiguration,
Label,
LocalizedLabel,
LookupAttributeMetadata,
ManyToManyRelationshipMetadata,
OneToManyRelationshipMetadata,
)
# Create a one-to-many relationship: Department (1) -> Employee (N)
# This adds a "Department" lookup field to the Employee table
lookup = LookupAttributeMetadata(
schema_name="new_DepartmentId",
display_name=Label(localized_labels=[LocalizedLabel(label="Department", language_code=1033)]),
)
relationship = OneToManyRelationshipMetadata(
schema_name="new_Department_Employee",
referenced_entity="new_department", # Parent table (the "one" side)
referencing_entity="new_employee", # Child table (the "many" side)
referenced_attribute="new_departmentid",
)
result = client.tables.create_one_to_many_relationship(lookup, relationship)
print(f"Created lookup field: {result.lookup_schema_name}")
# Create a many-to-many relationship: Employee (N) <-> Project (N)
# Employees work on multiple projects; projects have multiple team members
m2m_relationship = ManyToManyRelationshipMetadata(
schema_name="new_employee_project",
entity1_logical_name="new_employee",
entity2_logical_name="new_project",
)
result = client.tables.create_many_to_many_relationship(m2m_relationship)
print(f"Created M:N relationship: {result.relationship_schema_name}")
# Query relationship metadata
rel = client.tables.get_relationship("new_Department_Employee")
if rel:
print(f"Found: {rel.relationship_schema_name}")
# List all relationships
rels = client.tables.list_relationships()
for rel in rels:
print(f"{rel['SchemaName']} ({rel.get('RelationshipType')})")
# List relationships for a specific table (one-to-many + many-to-one + many-to-many)
account_rels = client.tables.list_table_relationships("account")
for rel in account_rels:
print(f"{rel['SchemaName']} -> {rel.get('RelationshipType')}")
# Delete a relationship
client.tables.delete_relationship(result.relationship_id)
对于更简单的方案,请使用便捷方法。
# Quick way to create a lookup field with sensible defaults
result = client.tables.create_lookup_field(
referencing_table="contact", # Child table gets the lookup field
lookup_field_name="new_AccountId",
referenced_table="account", # Parent table being referenced
display_name="Account",
)
有关完整的工作示例,请参阅 示例/高级/relationships.py。
配置级联行为
CascadeConfiguration 决定在一对多关系中对父记录执行操作时,子记录会发生什么。 以下值对于每个级联属性(assign、、、deletemergereparent、share、、 ) unshare都有效。
| 价值 | Behavior |
|---|---|
"Cascade" |
对所有关联的子记录执行该操作。 |
"NoCascade" |
不要将该操作应用到任何子记录。 |
"RemoveLink" |
删除父记录时,删除所有子记录上的查阅字段值。 |
"Restrict" |
存在子记录时,防止删除父记录。 |
默认情况下,delete为"RemoveLink",所有其他属性均为"NoCascade"。 可以导入常量以直接使用字符串值。
from PowerPlatform.Dataverse.common.constants import (
CASCADE_BEHAVIOR_CASCADE,
CASCADE_BEHAVIOR_NO_CASCADE,
CASCADE_BEHAVIOR_REMOVE_LINK,
CASCADE_BEHAVIOR_RESTRICT,
)
relationship = OneToManyRelationshipMetadata(
schema_name="new_Department_Employee",
referenced_entity="new_department",
referencing_entity="new_employee",
referenced_attribute="new_departmentid",
cascade_configuration=CascadeConfiguration(delete=CASCADE_BEHAVIOR_REMOVE_LINK),
)
RelationshipInfo 返回对象
关系创建方法返回具有以下 RelationshipInfo 字段的对象。
| 领域 | Description |
|---|---|
relationship_id |
关系元数据的 GUID。 将此值传递给 delete_relationship。 |
relationship_schema_name |
关系的架构名称。 |
relationship_type |
"one_to_many" 或 "many_to_many"。 |
lookup_schema_name |
在子表上创建的查找字段的架构名称(仅适用于一对多)。 |
referenced_entity / referencing_entity |
父表及子表逻辑名称(一对多) |
entity1_logical_name / entity2_logical_name |
两个表的逻辑名称(多对多)。 |
注释
如果不为多对多关系指定 intersect_entity_name,则联结表将使用该关系的 schema_name 作为其名称。
create_lookup_field 选项
create_lookup_field便利方法接受以下可选参数。
| 参数 | 违约 | Description |
|---|---|---|
display_name |
引用的表名称 | 查找字段显示的名称。 |
description |
None |
查找字段的可选说明。 |
required |
False |
查找字段是否为必填项。 |
cascade_delete |
"RemoveLink" |
删除级联行为: "RemoveLink"、 "Cascade"或 "Restrict"。 |
language_code |
1033 |
生成的标签的语言代码(LCID)。 |
solution |
None |
用于关联关系的解决方案唯一名称。 |
result = client.tables.create_lookup_field(
referencing_table="new_order",
lookup_field_name="new_AccountId",
referenced_table="account",
display_name="Account",
required=True,
cascade_delete="RemoveLink",
)
Important
删除一对多关系还会从子表中删除关联的查找字段。 此操作不可逆。 必须先删除关系,然后才能删除它们连接的表。
list_table_relationships 如果指定的表不存在,则引发 MetadataError。