自定义表和列

SDK 支持对 自定义表 和列执行创建、更新和删除(CUD)操作,支持可选的解决方案关联,以及检索和列出表定义。

让我们看看使用自定义表的示例代码。

# Create a custom table, including the customization prefix value in the schema names for the table and columns.
table_info = client.tables.create("new_Product", {
    "new_Code": "string",
    "new_Description": "memo",
    "new_Price": "decimal",
    "new_Active": "bool"
})

# Create with custom primary column name and solution assignment
table_info = client.tables.create(
    "new_Product",
    columns={
        "new_Code": "string",
        "new_Price": "decimal"
    },
    solution="MyPublisher",  # Optional: add to specific solution
    primary_column="new_ProductName",  # Optional: custom primary column (default is "{customization prefix value}_Name")
)

# Get table information
info = client.tables.get("new_Product")
print(f"Logical name: {info['table_logical_name']}")
print(f"Entity set: {info['entity_set_name']}")

# List all tables
tables = client.tables.list()
for table in tables:
    print(table)

# Add columns to existing table (columns must include customization prefix value)
client.tables.add_columns("new_Product", {"new_Category": "string"})

# Remove columns
client.tables.remove_columns("new_Product", ["new_Category"])

# List all columns (attributes) for a table to discover schema
columns = client.tables.list_columns("account")
for col in columns:
    print(f"{col['name']} ({col.get('AttributeType')})")

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

# Clean up
client.tables.delete("new_Product")

支持的列类型

create()add_columns() 接受以下类型字符串。

类型 接受的别名
string text
memo multiline
int integer
decimal money
float double
bool boolean
datetime date
file

对于选项集(choice)列,应直接将 Enum 的子类(或其成员具有整数值的 IntEnum)作为列类型值传递,而不是传递字符串。 SDK 使用类成员定义选项集值。

from enum import IntEnum

class Priority(IntEnum):
    LOW = 1
    MEDIUM = 2
    HIGH = 3

table_info = client.tables.create("new_Task", {
    "new_Title": "string",
    "new_Priority": Priority,   # optionset column
})

TableInfo 返回对象

方法 client.tables.create() 返回 对象 TableInfo 。 直接访问其属性,或使用旧版字典键表示法以保持向后兼容性。

table_info = client.tables.create("new_Product", {"new_Code": "string"})

print(table_info.schema_name)       # new_Product
print(table_info.logical_name)      # new_product
print(table_info.entity_set_name)   # new_products
print(table_info.columns_created)   # ['new_Code', ...]

# Legacy dict-key access still works
print(table_info["table_schema_name"])

add_columns()remove_columns()方法返回它们创建或删除的列架构名称的列表。 该方法 get() 返回表元数据,或者 None 如果表不存在,这使得它可用于存在检查。

替换键

备用键使用一个或多个业务列来标识记录,而不是使用 Dataverse 生成的 GUID。 upsert 操作需要备用键。 在Power Apps创建者门户中的>下定义它们,或者通过使用client.tables.create_alternate_key编程方式定义它们。

# Create an alternate key on the accountnumber column
key = client.tables.create_alternate_key(
    "account",
    "account_accountnumber_ak",
    ["accountnumber"],
    display_name="Account Number",
)
print(f"Created key {key.schema_name} ({key.metadata_id}), status={key.status}")

# The key status transitions from Pending to Active asynchronously - poll before upserting
for k in client.tables.get_alternate_keys("account"):
    if k.schema_name == "account_accountnumber_ak":
        print(f"{k.schema_name}: {k.status}")

Important

Pending 过渡到 Active 不是即时的。 在创建后立即检查密钥状态,并等待其变为 Active 后再发出 upsert 请求。 如果没有活动的备用密钥,Dataverse 将拒绝出现 400 错误的 upsert 请求。

Important

所有自定义列名都必须包括自定义前缀值(例如“new_”)。 此要求可确保显式、可预测的命名,并符合 Dataverse 元数据要求。

有关处理自定义表元数据的详细信息:

  • create始终返回 GUID 列表(单条输入时长度=1)。
  • updatedelete 对于单个接口和多个接口返回 None
  • create 传递有效负载列表将触发批量创建,并返回 list[str] 个 ID。
  • get 支持通过记录 ID 检索单条记录或分页浏览结果集(建议使用 select 限制列)。
  • 对于接受记录 ID 作为参数的 CRUD 方法,请传递 36 个字符、带连字符的 GUID 字符串。 GUID 周围的括号是允许的,但不是必须的。

另见