All Products
Search
Document Center

Tablestore:Use auto-increment primary key columns

Last Updated:Aug 04, 2026

Use Tablestore SDK for Python to configure an auto-increment primary key column and obtain its generated value when you write data.

Prerequisites

Install the Tablestore SDK for Python and initialize a client. Auto-increment primary key columns require version 4.0.0 or later. We recommend that you use the latest version.

Function description

Tablestore generates a signed 64-bit integer for an auto-increment primary key column. Generated values are unique and strictly increasing within the same partition key but may not be consecutive.

Configure an auto-increment primary key column

When you create a table, set a non-partition primary key column of the INTEGER type to PK_AUTO_INCR. Each table can have at most one auto-increment primary key column. You cannot add one after the table is created.

The following example creates example_table with the partition key id and the auto-increment primary key column sequence_id.

table_meta = TableMeta(
    "example_table",
    [
        ("id", "STRING"),
        ("sequence_id", "INTEGER", PK_AUTO_INCR),
    ],
)
table_options = TableOptions()
reserved_throughput = ReservedThroughput(CapacityUnit(0, 0))

client.create_table(table_meta, table_options, reserved_throughput)

Write data and obtain the generated value

When you write data, set the value of the auto-increment primary key column to PK_AUTO_INCR. To use the generated value in subsequent queries or updates, set return_type to ReturnType.RT_PK.

primary_key = [
    ("id", "device-001"),
    ("sequence_id", PK_AUTO_INCR),
]
attribute_columns = [("status", "online")]
row = Row(primary_key, attribute_columns)

_, return_row = client.put_row(
    "example_table",
    row,
    return_type=ReturnType.RT_PK,
)

print(return_row.primary_key)

Parameters

Auto-increment primary key column configuration

The tuple in schema_of_primary_key[] that configures an auto-increment primary key column contains the following elements.

Name

Type

Description

name (required)

str

The name of the primary key column.

type (required)

str

The type of the primary key column. Set the value to INTEGER.

option (required)

PrimaryKeyOption

The primary key option. Set the value to PK_AUTO_INCR. An auto-increment primary key column cannot be the partition key.

Write parameters

The put_row method contains the following parameters that are related to auto-increment primary key columns.

Name

Type

Description

table_name (required)

str

The name of the table.

row (required)

Row

The row to write. Set the value of the auto-increment primary key column to PK_AUTO_INCR.

return_type (optional)

ReturnType

The response type. Set the value to RT_PK to return the primary key of the written row. If you do not specify this parameter, the primary key is not returned.

Response

The put_row method returns a tuple that contains the following information.

Field

Type

Description

consumed

CapacityUnit

The read and write CUs consumed by the operation.

return_row

Row

If return_type is set to RT_PK, this field contains the complete primary key generated by Tablestore. Otherwise, the value is None.