TableOperations 类

表级元数据操作的命名空间。

通过 . 访问 。client.tables 提供创建、删除、检查和列出 Dataverse 表以及添加和删除列的操作。

例:


   client = DataverseClient(base_url, credential)

   # Create a table
   info = client.tables.create(
       "new_Product",
       {"new_Price": "decimal", "new_InStock": "bool"},
       solution="MySolution",
   )

   # List tables
   tables = client.tables.list()

   # Get table info
   info = client.tables.get("new_Product")

   # Add columns
   client.tables.add_columns("new_Product", {"new_Rating": "int"})

   # Remove columns
   client.tables.remove_columns("new_Product", "new_Rating")

   # Delete a table
   client.tables.delete("new_Product")

构造函数

TableOperations(client: DataverseClient)

参数

名称 说明
client
必需

DataverseClient 实例。

方法

add_columns

向现有表添加一个或多个列。

例:


   created = client.tables.add_columns(
       "new_MyTestTable",
       {"new_Notes": "string", "new_Active": "bool"},
   )
   print(created)  # ['new_Notes', 'new_Active']
create

使用指定的列创建自定义表。

create_alternate_key

在表上创建备用键。

备用键允许 upsert 操作按一个或多个列(而不是主 GUID)标识记录。 创建密钥后,将排队用于索引生成;索引准备就绪后,它将 status"Pending" 过渡到 "Active" 该索引。

create_lookup_field

创建简单的查阅字段关系。

这是一种方便的方法,用于包装 create_one_to_many_relationship 向现有表添加查阅字段的常见情况。

create_many_to_many_relationship

在表之间创建多对多关系。

此操作将创建一个多对多关系和一个相交表来管理关系。

create_one_to_many_relationship

在表之间创建一对多关系。

此操作在引用表上创建关系和查找属性。

delete

按架构名称删除自定义表。

警告

此操作不可逆,将删除

表以及表定义。

例:


   client.tables.delete("new_MyTestTable")
delete_alternate_key

按其元数据 ID 删除备用密钥。

警告

删除 upsert 操作正在使用的备用密钥将

导致这些操作失败。 此操作不可逆。

例:


   client.tables.delete_alternate_key(
       "new_Product",
       "12345678-1234-1234-1234-123456789abc",
   )
delete_relationship

按其元数据 ID 删除关系。

警告

删除关系还会删除关联的查找属性

用于一对多关系。 此操作不可逆。

例:


   client.tables.delete_relationship(
       "12345678-1234-1234-1234-123456789abc"
   )
get

获取表的基本元数据(如果存在)。

例:


   info = client.tables.get("new_MyTestTable")
   if info:
       print(f"Logical name: {info['table_logical_name']}")
       print(f"Entity set: {info['entity_set_name']}")
get_alternate_keys

列出表上定义的所有备用键。

get_relationship

按架构名称检索关系元数据。

例:


   rel = client.tables.get_relationship("new_Department_Employee")
   if rel:
       print(f"Found: {rel.relationship_schema_name}")
list

列出 Dataverse 环境中的所有非专用表。

默认情况下,返回每个表,其中 IsPrivate eq false。 提供可选的 OData $filter 表达式以进一步缩小结果范围。 表达式与使用IsPrivate eq false的默认and子句结合使用。

例:


   # List all non-private tables
   tables = client.tables.list()
   for table in tables:
       print(table["LogicalName"])

   # List only tables whose schema name starts with "new_"
   custom_tables = client.tables.list(
       filter="startswith(SchemaName, 'new_')"
   )

   # List tables with only specific properties
   tables = client.tables.list(
       select=["LogicalName", "SchemaName", "EntitySetName"]
   )
list_columns

列出表的所有属性(列)定义。

例:


   # List all columns on the account table
   columns = client.tables.list_columns("account")
   for col in columns:
       print(f"{col['LogicalName']} ({col.get('AttributeType')})")

   # List only specific properties
   columns = client.tables.list_columns(
       "account",
       select=["LogicalName", "SchemaName", "AttributeType"],
   )

   # Filter to only string attributes
   columns = client.tables.list_columns(
       "account",
       filter="AttributeType eq 'String'",
   )
list_relationships

列出环境中的所有关系定义。

例:


   # List all relationships
   rels = client.tables.list_relationships()
   for rel in rels:
       print(f"{rel['SchemaName']} ({rel.get('@odata.type')})")

   # Filter by type
   one_to_many = client.tables.list_relationships(
       filter="RelationshipType eq Microsoft.Dynamics.CRM.RelationshipType'OneToManyRelationship'"
   )

   # Select specific properties
   rels = client.tables.list_relationships(
       select=["SchemaName", "ReferencedEntity", "ReferencingEntity"]
   )
list_table_relationships

列出特定表的所有关系。

通过查询和组合EntityDefinitions({id})/OneToManyRelationshipsEntityDefinitions({id})/ManyToOneRelationshipsEntityDefinitions({id})/ManyToManyRelationships给定表的一对多、多对一和多对多关系。

例:


   # List all relationships for the account table
   rels = client.tables.list_table_relationships("account")
   for rel in rels:
       print(f"{rel['SchemaName']} -> {rel.get('@odata.type')}")
remove_columns

从表中删除一个或多个列。

例:


   removed = client.tables.remove_columns(
       "new_MyTestTable",
       ["new_Notes", "new_Active"],
   )
   print(removed)  # ['new_Notes', 'new_Active']

add_columns

向现有表添加一个或多个列。

例:


   created = client.tables.add_columns(
       "new_MyTestTable",
       {"new_Notes": "string", "new_Active": "bool"},
   )
   print(created)  # ['new_Notes', 'new_Active']
add_columns(table: str, columns: Dict[str, Any]) -> List[str]

参数

名称 说明
table
必需
str

表的架构名称(例如 "new_MyTestTable")。

columns
必需

列架构名称(带有自定义前缀)映射到其类型。 支持的类型与 <a0/> 的类型相同。

返回

类型 说明

创建的列的架构名称。

例外

类型 说明

如果该表不存在。

create

使用指定的列创建自定义表。

create(table: str, columns: Dict[str, Any], *, solution: str | None = None, primary_column: str | None = None, display_name: str | None = None) -> TableInfo

参数

名称 说明
table
必需
str

具有自定义前缀的表的架构名称(例如 "new_MyTestTable")。

columns
必需

列架构名称(带有自定义前缀)映射到其类型。 支持的类型包括"string"(或)、(或"text")、"integer""money""memo""float""decimal""int" (或"multiline")、(或"double""datetime")、(或)、(或)、 "bool" (或"boolean""date""file"Enum子类(对于本地选项集)。

solution
必需
str

应拥有新表的可选解决方案唯一名称。 在默认解决方案中省略表时。

primary_column
必需
str

可选主名称列架构名称(如 "new_ProductName"自定义前缀)。 如果未提供,则默认为 "{prefix}_Name".

display_name
必需
str

表的可读显示名称(例如 "Product")。 省略时,默认为表架构名称。

仅限关键字的参数

名称 说明
solution
默认值: None
primary_column
默认值: None
display_name
默认值: None

返回

类型 说明

具有 schema_name、、 metadata_identity_set_namelogical_name和 . 的columns_created表元数据。 支持使用旧密钥进行类似听写的访问,以实现向后兼容性。

例外

类型 说明

如果创建表失败或表已存在。

示例

创建包含简单列的表:


   from enum import IntEnum

   class ItemStatus(IntEnum):
       ACTIVE = 1
       INACTIVE = 2

   result = client.tables.create(
       "new_Product",
       {
           "new_Title": "string",
           "new_Price": "decimal",
           "new_Status": ItemStatus,
       },
       solution="MySolution",
       primary_column="new_ProductName",
       display_name="Product",
   )
   print(f"Created: {result['table_schema_name']}")

create_alternate_key

在表上创建备用键。

备用键允许 upsert 操作按一个或多个列(而不是主 GUID)标识记录。 创建密钥后,将排队用于索引生成;索引准备就绪后,它将 status"Pending" 过渡到 "Active" 该索引。

create_alternate_key(table: str, key_name: str, columns: List[str], *, display_name: str | None = None, language_code: int = 1033) -> AlternateKeyInfo

参数

名称 说明
table
必需
str

表的架构名称(例如 "new_Product")。

key_name
必需
str

新备用键的架构名称(例如 "new_product_code_key")。

columns
必需

构成键的列逻辑名称列表(例如 ["new_productcode"])。

display_name
必需
str

密钥的显示名称。 如果未提供,则默认为 key_name

language_code
必需
int

标签的语言代码。 默认值为 1033(英语)。

仅限关键字的参数

名称 说明
display_name
默认值: None
language_code
默认值: 1033

返回

类型 说明

新创建的备用密钥的元数据。

例外

类型 说明

如果该表不存在。

如果 Web API 请求失败。

示例

为 upsert 创建单列备用键:


   key = client.tables.create_alternate_key(
       "new_Product",
       "new_product_code_key",
       ["new_productcode"],
       display_name="Product Code",
   )
   print(f"Key ID: {key.metadata_id}")
   print(f"Columns: {key.key_attributes}")

create_lookup_field

创建简单的查阅字段关系。

这是一种方便的方法,用于包装 create_one_to_many_relationship 向现有表添加查阅字段的常见情况。

create_lookup_field(referencing_table: str, lookup_field_name: str, referenced_table: str, *, display_name: str | None = None, description: str | None = None, required: bool = False, cascade_delete: str = 'RemoveLink', solution: str | None = None, language_code: int = 1033) -> RelationshipInfo

参数

名称 说明
referencing_table
必需
str

具有查阅字段(子表)的表的逻辑名称。

lookup_field_name
必需
str

查找字段的架构名称(例如 "new_AccountId")。

referenced_table
必需
str

要引用的表的逻辑名称(父表)。

display_name
必需
str

查找字段的显示名称。 默认为引用的表名称。

description
必需
str

查找字段的可选说明。

required
必需

是否需要查找。 默认为 False

cascade_delete
必需
str

删除行为("RemoveLink"、、"Cascade""Restrict")。 默认为 "RemoveLink"

solution
必需
str

要向其添加关系的可选解决方案唯一名称。

language_code
必需
int

标签的语言代码。 默认值为 1033(英语)。

仅限关键字的参数

名称 说明
display_name
默认值: None
description
默认值: None
required
默认值: False
cascade_delete
默认值: RemoveLink
solution
默认值: None
language_code
默认值: 1033

返回

类型 说明

relationship_id、、relationship_schema_namerelationship_typelookup_schema_namereferenced_entity和的关系referencing_entity元数据。

例外

类型 说明

如果 Web API 请求失败。

示例

创建简单的查阅字段:


   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=CASCADE_BEHAVIOR_REMOVE_LINK,
   )
   print(f"Created lookup: {result.lookup_schema_name}")

create_many_to_many_relationship

在表之间创建多对多关系。

此操作将创建一个多对多关系和一个相交表来管理关系。

create_many_to_many_relationship(relationship: ManyToManyRelationshipMetadata, *, solution: str | None = None) -> RelationshipInfo

参数

名称 说明
relationship
必需

定义多对多关系的元数据。

solution
必需
str

要向其添加关系的可选解决方案唯一名称。

仅限关键字的参数

名称 说明
solution
默认值: None

返回

类型 说明

relationship_id、、relationship_schema_namerelationship_typeentity1_logical_nameentity2_logical_name的关系元数据。

例外

类型 说明

如果 Web API 请求失败。

示例

创建多对多关系:员工 <-> 项目:


   from PowerPlatform.Dataverse.models import (
       ManyToManyRelationshipMetadata,
   )

   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(relationship)
   print(f"Created: {result.relationship_schema_name}")

create_one_to_many_relationship

在表之间创建一对多关系。

此操作在引用表上创建关系和查找属性。

create_one_to_many_relationship(lookup: LookupAttributeMetadata, relationship: OneToManyRelationshipMetadata, *, solution: str | None = None) -> RelationshipInfo

参数

名称 说明
lookup
必需

定义查找属性的元数据。

relationship
必需

定义关系的元数据。

solution
必需
str

要向其添加关系的可选解决方案唯一名称。

仅限关键字的参数

名称 说明
solution
默认值: None

返回

类型 说明

relationship_id、、relationship_schema_namerelationship_typelookup_schema_namereferenced_entity和的关系referencing_entity元数据。

例外

类型 说明

如果 Web API 请求失败。

示例

创建一对多关系:部门(1) -> 员工(N):


   from PowerPlatform.Dataverse.models import (
       LookupAttributeMetadata,
       OneToManyRelationshipMetadata,
       Label,
       LocalizedLabel,
       CascadeConfiguration,
   )
   from PowerPlatform.Dataverse.common.constants import (
       CASCADE_BEHAVIOR_REMOVE_LINK,
   )

   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",
       referencing_entity="new_employee",
       referenced_attribute="new_departmentid",
       cascade_configuration=CascadeConfiguration(
           delete=CASCADE_BEHAVIOR_REMOVE_LINK,
       ),
   )

   result = client.tables.create_one_to_many_relationship(lookup, relationship)
   print(f"Created lookup field: {result.lookup_schema_name}")

delete

按架构名称删除自定义表。

警告

此操作不可逆,将删除

表以及表定义。

例:


   client.tables.delete("new_MyTestTable")
delete(table: str) -> None

参数

名称 说明
table
必需
str

表的架构名称(例如 "new_MyTestTable")。

例外

类型 说明

如果该表不存在或删除失败。

delete_alternate_key

按其元数据 ID 删除备用密钥。

警告

删除 upsert 操作正在使用的备用密钥将

导致这些操作失败。 此操作不可逆。

例:


   client.tables.delete_alternate_key(
       "new_Product",
       "12345678-1234-1234-1234-123456789abc",
   )
delete_alternate_key(table: str, key_id: str) -> None

参数

名称 说明
table
必需
str

表的架构名称(例如 "new_Product")。

key_id
必需
str

要删除的备用密钥的元数据 GUID。

例外

类型 说明

如果该表不存在。

如果 Web API 请求失败。

delete_relationship

按其元数据 ID 删除关系。

警告

删除关系还会删除关联的查找属性

用于一对多关系。 此操作不可逆。

例:


   client.tables.delete_relationship(
       "12345678-1234-1234-1234-123456789abc"
   )
delete_relationship(relationship_id: str) -> None

参数

名称 说明
relationship_id
必需
str

关系元数据的 GUID。

例外

类型 说明

如果 Web API 请求失败。

get

获取表的基本元数据(如果存在)。

例:


   info = client.tables.get("new_MyTestTable")
   if info:
       print(f"Logical name: {info['table_logical_name']}")
       print(f"Entity set: {info['entity_set_name']}")
get(table: str) -> TableInfo | None

参数

名称 说明
table
必需
str

表的架构名称(例如 "new_MyTestTable""account")。

返回

类型 说明

表元数据,或者 None 找不到表。 支持使用旧密钥进行类似听写的访问,以实现向后兼容性。

get_alternate_keys

列出表上定义的所有备用键。

get_alternate_keys(table: str) -> List[AlternateKeyInfo]

参数

名称 说明
table
必需
str

表的架构名称(例如 "new_Product")。

返回

类型 说明

备用键元数据对象列表。 如果未定义备用键,则为空。

例外

类型 说明

如果该表不存在。

如果 Web API 请求失败。

示例

列出备用键并打印其状态:


   keys = client.tables.get_alternate_keys("new_Product")
   for key in keys:
       print(f"{key.schema_name}: {key.status}")

get_relationship

按架构名称检索关系元数据。

例:


   rel = client.tables.get_relationship("new_Department_Employee")
   if rel:
       print(f"Found: {rel.relationship_schema_name}")
get_relationship(schema_name: str) -> RelationshipInfo | None

参数

名称 说明
schema_name
必需
str

关系模式名称。

返回

类型 说明

关系元数据,或者 None 找不到。

例外

类型 说明

如果 Web API 请求失败。

list

列出 Dataverse 环境中的所有非专用表。

默认情况下,返回每个表,其中 IsPrivate eq false。 提供可选的 OData $filter 表达式以进一步缩小结果范围。 表达式与使用IsPrivate eq false的默认and子句结合使用。

例:


   # List all non-private tables
   tables = client.tables.list()
   for table in tables:
       print(table["LogicalName"])

   # List only tables whose schema name starts with "new_"
   custom_tables = client.tables.list(
       filter="startswith(SchemaName, 'new_')"
   )

   # List tables with only specific properties
   tables = client.tables.list(
       select=["LogicalName", "SchemaName", "EntitySetName"]
   )
list(*, filter: str | None = None, select: List[str] | None = None) -> List[Dict[str, Any]]

参数

名称 说明
filter
必需
str

可选 OData $filter 表达式,以进一步缩小返回的表列表(例如 "SchemaName eq 'Account'")。 筛选器表达式中的列名必须使用元数据(通常是 PascalCase)中的 EntityDefinitions 确切属性名称。

select
必需
list[str] 或 None

要包含在响应中的属性名称的可选列表(通过 OData $select 查询选项投影)。 属性名称必须使用元数据中确切的 EntityDefinitions PascalCase 名称(例如 ["LogicalName", "SchemaName", "DisplayName"])。 当(默认值)或空列表时 None ,将返回所有属性。

仅限关键字的参数

名称 说明
filter
默认值: None
select
默认值: None

返回

类型 说明

EntityDefinition 元数据字典的列表。

list_columns

列出表的所有属性(列)定义。

例:


   # List all columns on the account table
   columns = client.tables.list_columns("account")
   for col in columns:
       print(f"{col['LogicalName']} ({col.get('AttributeType')})")

   # List only specific properties
   columns = client.tables.list_columns(
       "account",
       select=["LogicalName", "SchemaName", "AttributeType"],
   )

   # Filter to only string attributes
   columns = client.tables.list_columns(
       "account",
       filter="AttributeType eq 'String'",
   )
list_columns(table: str, *, select: List[str] | None = None, filter: str | None = None) -> List[Dict[str, Any]]

参数

名称 说明
table
必需
str

表的架构名称(例如 "account""new_Product")。

select
必需
list[str] 或 None

要通过 $select. 进行投影的属性名称的可选列表。 值 as-is 传递(PascalCase)。

filter
必需
str

可选 OData $filter 表达式。 例如, "AttributeType eq 'String'" 仅返回字符串列。

仅限关键字的参数

名称 说明
select
默认值: None
filter
默认值: None

返回

类型 说明

原始属性元数据字典的列表。

例外

类型 说明

如果未找到该表。

如果 Web API 请求失败。

list_relationships

列出环境中的所有关系定义。

例:


   # List all relationships
   rels = client.tables.list_relationships()
   for rel in rels:
       print(f"{rel['SchemaName']} ({rel.get('@odata.type')})")

   # Filter by type
   one_to_many = client.tables.list_relationships(
       filter="RelationshipType eq Microsoft.Dynamics.CRM.RelationshipType'OneToManyRelationship'"
   )

   # Select specific properties
   rels = client.tables.list_relationships(
       select=["SchemaName", "ReferencedEntity", "ReferencingEntity"]
   )
list_relationships(*, filter: str | None = None, select: List[str] | None = None) -> List[Dict[str, Any]]

参数

名称 说明
filter
必需
str

可选 OData $filter 表达式。 例如: "RelationshipType eq Microsoft.Dynamics.CRM.RelationshipType'OneToManyRelationship'"仅返回一对多关系。

select
必需
list[str] 或 None

要通过 $select. 进行投影的属性名称的可选列表。 值 as-is 传递(PascalCase)。

仅限关键字的参数

名称 说明
filter
默认值: None
select
默认值: None

返回

类型 说明

原始关系元数据字典的列表。

例外

类型 说明

如果 Web API 请求失败。

list_table_relationships

列出特定表的所有关系。

通过查询和组合EntityDefinitions({id})/OneToManyRelationshipsEntityDefinitions({id})/ManyToOneRelationshipsEntityDefinitions({id})/ManyToManyRelationships给定表的一对多、多对一和多对多关系。

例:


   # List all relationships for the account table
   rels = client.tables.list_table_relationships("account")
   for rel in rels:
       print(f"{rel['SchemaName']} -> {rel.get('@odata.type')}")
list_table_relationships(table: str, *, filter: str | None = None, select: List[str] | None = None) -> List[Dict[str, Any]]

参数

名称 说明
table
必需
str

表的架构名称(例如 "account")。

filter
必需
str

应用于每个子请求的可选 OData $filter 表达式。

select
必需
list[str] 或 None

要通过 $select. 进行投影的属性名称的可选列表。 值 as-is 传递(PascalCase)。

仅限关键字的参数

名称 说明
filter
默认值: None
select
默认值: None

返回

类型 说明

一对多、多对一和多对多关系元数据字典的组合列表。

例外

类型 说明

如果未找到该表。

如果 Web API 请求失败。

remove_columns

从表中删除一个或多个列。

例:


   removed = client.tables.remove_columns(
       "new_MyTestTable",
       ["new_Notes", "new_Active"],
   )
   print(removed)  # ['new_Notes', 'new_Active']
remove_columns(table: str, columns: str | List[str]) -> List[str]

参数

名称 说明
table
必需
str

表的架构名称(例如 "new_MyTestTable")。

columns
必需
strlist[str]

要删除的列架构名称或列架构名称的列表。 必须包含自定义前缀(例如 "new_TestColumn")。

返回

类型 说明

已删除的列的架构名称。

例外

类型 说明

如果表或指定的列不存在。