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

如何在GridDB Cloud集合容器中创建非空列?

如何在GridDB Cloud集合容器中创建非空列?

嘿,我来帮你搞定这个问题!你遇到的情况是因为GridDB Cloud的REST API在定义列时,nullable(是否允许空值)属性默认是true——只有作为rowkey的主键列会被自动设为非空,其他列如果不明确指定,就会默认允许NULL值。

要解决这个问题超简单,只需要在每个需要设为非空的列定义里,手动添加"nullable": false这个参数就可以了。

下面是修改后的完整Python脚本,我标记了新增的关键配置:

#CreateOrderTable.py
import requests
import json

# Set table name
tableName = "Orders"

# URL and authentication
url = "https://cloud5197.griddb.com:443/griddb/v2/gs_clustermfcloud5197/dbs/s4Rqrm4g/containers"

headers = {
  'Content-Type': 'application/json',
  'Authorization': 'Basic **************************',
  'User-Agent': 'PostmanRuntime/7.29.0'
}

# If table exists, drop it...
payload = json.dumps([
  tableName
])
response = requests.request("DELETE", url, headers=headers, data=payload)
print("Drop table result: ", response.status_code)

# Create table Order
payload = json.dumps({
  "container_name": tableName,
  "container_type": "COLLECTION",
  "rowkey": True,
  "columns": [
    {
      "name": "Order_ID",
      "type": "INTEGER",
      # 主键作为rowkey自动非空,这个配置可加可不加
      "nullable": False
    },
    {
      "name": "Order_Date",
      "type": "TIMESTAMP",
      "nullable": False  # 新增:明确禁止该列空值
    },
    {
      "name": "Client_ID",
      "type": "INTEGER",
      "nullable": False  # 新增:明确禁止该列空值
    },
    {
      "name": "Order_NF",
      "type": "STRING",
      "nullable": False  # 新增:明确禁止该列空值
    }
  ]
})
response = requests.request("POST", url, headers=headers, data=payload)
print("Create table result: ", response.status_code)

关键说明:

  1. 主键Order_ID因为是rowkey(脚本里设了"rowkey": True),GridDB会自动将其设为非空,你可以选择是否显式添加"nullable": False,不影响最终效果。
  2. 其他列必须手动添加"nullable": False,这样创建后的集合容器就会强制要求这些列不能为NULL。
  3. 运行修改后的脚本前,确保旧的表已经被成功删除(你的脚本里已经包含了删除逻辑,这部分没问题),新的配置才能生效。

之后你可以尝试插入一条缺少Order_Date、Client_ID或Order_NF的测试数据,如果GridDB返回错误,就说明非空限制已经成功生效啦!

备注:内容来源于stack exchange,提问作者Danilo Silva

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.13 20:04:37