[Python] I need to create a compound primary key in a GridDB Cloud collection container

Discussão em 'Python' iniciado por Stack, Março 27, 2025.

  1. Stack

    Stack Membro Participativo

    I´m trying to create a table (collection container) using GridDB Cloud, using a Python script, as shown in GridDB Developers Quick Start Guide, available at https://griddb.net/en/blog/griddb-cloud-quick-start-guide/. The script runs fine, but the primary key is always created on the first column (Order_ID). However, I´d like the primary key to be based on both Order_ID and Product_ID columns. Any ideas on how to accomplish that? The Python script that drops and creates the table is shown below:

    #CreateOrderDetailsTable.py
    import requests
    import json

    # Set table name
    tableName = "OrderDetails"

    # URL and authentication
    url = "https://cloud5197.griddb.com:443/griddb/v2/gs_clustermfcloud****/dbs/******/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
    payload = json.dumps({
    "container_name": tableName,
    "container_type": "COLLECTION",
    "rowkey": True,
    "columns": [
    {
    "name": "Order_ID",
    "type": "INTEGER",

    },
    {
    "name": "Product_ID",
    "type": "INTEGER",

    },
    {
    "name": "Quantity",
    "type": "INTEGER",
    },
    {
    "name": "Price",
    "type": "FLOAT"
    }
    ]
    })
    response = requests.request("POST", url, headers=headers, data=payload)
    print("Create table result: ", response.status_code)

    Continue reading...

Compartilhe esta Página