Skip to main content
Version: User Guides (Cloud)

Primary Field & AutoID

The primary field uniquely identifies an entity. This page introduces how to add the primary field of two different data types and how to enable Zilliz Cloud to automatically allocate primary field values.

Overview

In a collection, the primary key of each entity should be globally unique. When adding the primary field, you need to explicitly set its data type to VARCHAR or INT64. Setting its data type to INT64 indicates that the primary keys should be an integer similar to 12345; Setting its data type to VARCHAR indicates that the primary keys should be a string similar to my_entity_1234.

You can also enable AutoID to make Zilliz Cloud automatically allocate primary keys for incoming entities. Once you have enabled AutoID in your collection, do not include primary keys when inserting entities.

The primary field in a collection does not have a default value and cannot be null.

Use Int64 Primary Keys

To use primary keys of the Int64 type, you need to set datatype to DataType.INT64 and set is_primary to true. If you also need Zilliz Cloud to allocate the primary keys for the incoming entities, also set auto_id to true.

from pymilvus import MilvusClient, DataType

schema = MilvusClient.create_schema()

schema.add_field(
field_name="my_id",
datatype=DataType.INT64,
is_primary=True,
auto_id=True,
)

Use VarChar Primary Keys

To use VarChar primary keys, in addition to changing the value of the data_type parameter to DataType.VARCHAR, you also need to set the max_length parameter for the field.

schema.add_field(
field_name="my_id",
datatype=DataType.VARCHAR,
is_primary=True,
auto_id=True,
max_length=512,
)