You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在同一用例中跨数据存储获取序列生成的ID

Solution to Reference Meta Table ID in NoSQL Perf Map

To resolve your issue of using the auto-generated meta_table.id as the key in the perf map of your NoSQL user record, follow these practical steps:

Step 1: Fetch the Meta Table ID After Insertion

After inserting the meta_table record in your RDBMS, add steps to fetch the record using its key (${tagId}_meta_table) and extract the generated ID into a pipeline variable.

Modify your pipeline's db1 section to include these new steps:

db1:
  # ... existing register, prepare, setup steps ...
  fetch_meta_id:
    datastore: db1
    action: dsunit:fetch
    table: meta_table
    key: ${tagId}_meta_table
    result: metaRecord
  set_meta_var:
    action: nop
    init:
      - metaId = $metaRecord.id

This stores the generated meta_table.id in the $metaId variable, making it available for use in the NoSQL setup.

Step 2: Dynamically Build the Perf Map in the Pipeline

Update the db2 section of your pipeline to modify the user record's perf map, replacing the placeholder with the $metaId variable as the key:

db2:
  # ... existing register step ...
  prepare:
    data:
      action: nop
      init:
        - db2key = data.db2.setup
        - db2Setup = $AsTableRecords($db2key)
        # Grab the user record from the setup data
        - userRecord = $db2Setup[0]
        # Create a new perf map with metaId as the key
        - perfMap = {}
        - perfMap[$metaId] = "$timestamp"
        # Replace the empty perf map in the user record
        - userRecord.Value.perf = $perfMap
        # Update the setup data array
        - db2Setup = [$userRecord]
  setup:
    datastore: db2
    action: dsunit:prepare
    data: $db2Setup

Step 3: Simplify Your db2_setup.json

Remove the placeholder from your db2_setup.json and leave an empty perf map (we'll populate it in the pipeline):

@db2_setup.json
[
  {
    "Table": "user",
    "Value": {
      "id": "$userID",
      "email": "user@abc.com",
      "perf": {}
    },
    "AutoGenerate": {
      "userID": "uuid.next"
    },
    "Key": "${tagId}_user"
  }
]

Alternative: Direct Variable Substitution in JSON Key

If your dsunit version supports variable substitution in JSON keys, you can skip the pipeline manipulation and directly use $metaId in your db2_setup.json:

@db2_setup.json
[
  {
    "Table": "user",
    "Value": {
      "id": "$userID",
      "email": "user@abc.com",
      "perf": {
        "$metaId": "$timestamp"
      }
    },
    "AutoGenerate": {
      "userID": "uuid.next"
    },
    "Key": "${tagId}_user"
  }
]

This works only if dsunit processes variable substitutions for both keys and values in the JSON data. If this doesn't work, stick with the pipeline manipulation approach.

Why Your Previous Attempts Failed

  • $meta_table.id isn't a valid variable because dsunit doesn't automatically expose table IDs as top-level variables. You need to explicitly fetch the record and extract the ID into a variable.
  • ${meta_table.id} has the same issue—there's no built-in variable named meta_table.id available in your pipeline unless you create it.

By fetching the record and storing the ID in a pipeline variable, you ensure the correct auto-generated ID is used as the key in your NoSQL perf map, even when a new ID is generated on each test run.

内容的提问来源于stack exchange,提问作者terry.zhao

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:40:51