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")
方法
| add_columns |
向现有表添加一个或多个列。 示例:
|
| create |
使用指定的列创建自定义表。 |
| create_alternate_key |
在表上创建备用键。 备用键允许 upsert 操作按一个或多个列(而不是主 GUID)标识记录。 创建密钥后,将排队用于索引生成;索引准备就绪后,它将 status 从 |
| create_lookup_field |
创建简单的查阅字段关系。 这是一种方便的方法,用于包装 create_one_to_many_relationship 向现有表添加查阅字段的常见情况。 |
| create_many_to_many_relationship |
在表之间创建多对多关系。 此操作将创建一个多对多关系和一个相交表来管理关系。 |
| create_one_to_many_relationship |
在表之间创建一对多关系。 此操作在引用表上创建关系和查找属性。 |
| delete |
按架构名称删除自定义表。 Warning 此操作不可逆,将删除 表以及表定义。 示例:
|
| delete_alternate_key |
按其元数据 ID 删除备用密钥。 Warning 删除 upsert 操作正在使用的备用密钥将 导致这些操作失败。 此操作不可逆。 示例:
|
| delete_relationship |
按其元数据 ID 删除关系。 Warning 删除关系还会删除关联的查找属性 用于一对多关系。 此操作不可逆。 示例:
|
| get |
获取表的基本元数据(如果存在)。 示例:
|
| get_alternate_keys |
列出表上定义的所有备用键。 |
| get_relationship |
按架构名称检索关系元数据。 示例:
|
| list |
列出 Dataverse 环境中的所有非专用表。 默认情况下,返回每个表,其中 示例:
|
| list_columns |
列出表的所有属性(列)定义。 示例:
|
| list_relationships |
列出环境中的所有关系定义。 示例:
|
| list_table_relationships |
列出特定表的所有关系。 通过查询和组合 示例:
|
| remove_columns |
从表中删除一个或多个列。 示例:
|
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
必需
|
表的架构名称(例如 |
|
columns
必需
|
列架构名称(带有自定义前缀)映射到其类型。 支持的类型与 < |
返回
| 类型 | 说明 |
|---|---|
|
创建的列的架构名称。 |
例外
| 类型 | 说明 |
|---|---|
|
如果该表不存在。 |
create
使用指定的列创建自定义表。
create(table: str, columns: Dict[str, Any], *, solution: str | None = None, primary_column: str | None = None, display_name: str | None = None) -> TableInfo
参数
| 名称 | 说明 |
|---|---|
|
table
必需
|
具有自定义前缀的表的架构名称(例如 |
|
columns
必需
|
列架构名称(带有自定义前缀)映射到其类型。 支持的类型包括 |
|
solution
必需
|
应拥有新表的可选解决方案唯一名称。 在默认解决方案中省略表时。 |
|
primary_column
必需
|
可选主名称列架构名称(如 |
|
display_name
必需
|
表的可读显示名称(例如 |
仅限关键字的参数
| 名称 | 说明 |
|---|---|
|
solution
|
默认值: None
|
|
primary_column
|
默认值: None
|
|
display_name
|
默认值: None
|
返回
| 类型 | 说明 |
|---|---|
|
具有 |
例外
| 类型 | 说明 |
|---|---|
|
如果创建表失败或表已存在。 |
示例
创建包含简单列的表:
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
必需
|
表的架构名称(例如 |
|
key_name
必需
|
新备用键的架构名称(例如 |
|
columns
必需
|
构成键的列逻辑名称列表(例如 |
|
display_name
必需
|
密钥的显示名称。 如果未提供,则默认为 |
|
language_code
必需
|
标签的语言代码。 默认值为 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
必需
|
具有查阅字段(子表)的表的逻辑名称。 |
|
lookup_field_name
必需
|
查找字段的架构名称(例如 |
|
referenced_table
必需
|
要引用的表的逻辑名称(父表)。 |
|
display_name
必需
|
查找字段的显示名称。 默认为引用的表名称。 |
|
description
必需
|
查找字段的可选说明。 |
|
required
必需
|
是否需要查找。 默认值为 |
|
cascade_delete
必需
|
删除行为( |
|
solution
必需
|
要向其添加关系的可选解决方案唯一名称。 |
|
language_code
必需
|
标签的语言代码。 默认值为 1033(英语)。 |
仅限关键字的参数
| 名称 | 说明 |
|---|---|
|
display_name
|
默认值: None
|
|
description
|
默认值: None
|
|
required
|
默认值: False
|
|
cascade_delete
|
默认值: RemoveLink
|
|
solution
|
默认值: None
|
|
language_code
|
默认值: 1033
|
返回
| 类型 | 说明 |
|---|---|
|
与 |
例外
| 类型 | 说明 |
|---|---|
|
如果 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
必需
|
要向其添加关系的可选解决方案唯一名称。 |
仅限关键字的参数
| 名称 | 说明 |
|---|---|
|
solution
|
默认值: None
|
返回
| 类型 | 说明 |
|---|---|
|
与 |
例外
| 类型 | 说明 |
|---|---|
|
如果 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
必需
|
要向其添加关系的可选解决方案唯一名称。 |
仅限关键字的参数
| 名称 | 说明 |
|---|---|
|
solution
|
默认值: None
|
返回
| 类型 | 说明 |
|---|---|
|
与 |
例外
| 类型 | 说明 |
|---|---|
|
如果 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
按架构名称删除自定义表。
Warning
此操作不可逆,将删除
表以及表定义。
示例:
client.tables.delete("new_MyTestTable")
delete(table: str) -> None
参数
| 名称 | 说明 |
|---|---|
|
table
必需
|
表的架构名称(例如 |
例外
| 类型 | 说明 |
|---|---|
|
如果该表不存在或删除失败。 |
delete_alternate_key
按其元数据 ID 删除备用密钥。
Warning
删除 upsert 操作正在使用的备用密钥将
导致这些操作失败。 此操作不可逆。
示例:
client.tables.delete_alternate_key(
"new_Product",
"12345678-1234-1234-1234-123456789abc",
)
delete_alternate_key(table: str, key_id: str) -> None
参数
| 名称 | 说明 |
|---|---|
|
table
必需
|
表的架构名称(例如 |
|
key_id
必需
|
要删除的备用密钥的元数据 GUID。 |
例外
| 类型 | 说明 |
|---|---|
|
如果该表不存在。 |
|
|
如果 Web API 请求失败。 |
delete_relationship
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
必需
|
表的架构名称(例如 |
返回
| 类型 | 说明 |
|---|---|
|
表元数据,或者 |
get_alternate_keys
列出表上定义的所有备用键。
get_alternate_keys(table: str) -> List[AlternateKeyInfo]
参数
| 名称 | 说明 |
|---|---|
|
table
必需
|
表的架构名称(例如 |
返回
| 类型 | 说明 |
|---|---|
|
备用键元数据对象列表。 如果未定义备用键,则为空。 |
例外
| 类型 | 说明 |
|---|---|
|
如果该表不存在。 |
|
|
如果 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
必需
|
关系模式名称。 |
返回
| 类型 | 说明 |
|---|---|
|
关系元数据,或者 |
例外
| 类型 | 说明 |
|---|---|
|
如果 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
必需
|
可选 OData |
|
select
必需
|
要包含在响应中的属性名称的可选列表(通过 OData |
仅限关键字的参数
| 名称 | 说明 |
|---|---|
|
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
必需
|
表的架构名称(例如 |
|
select
必需
|
要通过 |
|
filter
必需
|
可选 OData |
仅限关键字的参数
| 名称 | 说明 |
|---|---|
|
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
必需
|
可选 OData |
|
select
必需
|
要通过 |
仅限关键字的参数
| 名称 | 说明 |
|---|---|
|
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
必需
|
表的架构名称(例如 |
|
filter
必需
|
应用于每个子请求的可选 OData |
|
select
必需
|
要通过 |
仅限关键字的参数
| 名称 | 说明 |
|---|---|
|
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
必需
|
表的架构名称(例如 |
|
columns
必需
|
要删除的列架构名称或列架构名称的列表。 必须包含自定义前缀(例如 |
返回
| 类型 | 说明 |
|---|---|
|
已删除的列的架构名称。 |
例外
| 类型 | 说明 |
|---|---|
|
如果表或指定的列不存在。 |