Key Generation
Key Strategies
| Strategy | Usage on this datasource |
|---|---|
| Assigned | Use a collection without AutoID; repeated IDs do not raise a uniqueness error. |
| Auto-increment | Use AutoID; on the 2.6.2 baseline, do not mix automatic IDs with application-assigned IDs. |
| UUID | Use a VARCHAR primary key with AutoID disabled. |
| Sequence | No sequence-key strategy on this datasource. |
| Custom | Use a custom generator before or after insertion. |
Use a Generated ID
Define AUTO_ID on the collection primary key, then omit that column on insert:
CREATE TABLE auto_books (
id INT64 PRIMARY KEY AUTO_ID,
title VARCHAR(256),
book_intro FLOAT_VECTOR(2)
);
Provide a writable Long id property on AutoBook. The following Mapper definitions fill it after insertion:
- Method Annotations
- Mapper File
@Insert(value = "INSERT INTO auto_books (title, book_intro) VALUES (#{title}, #{bookIntro})",
useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
int insert(AutoBook item);
<insert id="insert" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
INSERT INTO auto_books (title, book_intro) VALUES (#{title}, #{bookIntro})
</insert>
After mapper.insert(item), read item.getId(). These examples pass one entity argument, not an argument wrapped in @Param.
The optional keyColumn="id" names the returned primary-key column. With the Builder API, configure KeyType.Auto for automatic backfill; see Key Generators.
Use an Assigned ID
For a collection without AUTO_ID, a primary-key value is required on insert. INSERT does not check for an existing primary key: inserting the same ID again does not raise a unique-key conflict.
For example, to replace the title of book 1001, use UPSERT. Do not detect its existence by attempting INSERT and catching a duplicate-key exception.
AUTO_ID Collections
For the Milvus 2.6.2 baseline, do not supply an application-assigned ID to an AutoID collection. Keep AutoID inserts and explicit-ID inserts in collections configured for the corresponding mode.
Generate a UUID
For a VARCHAR primary key with AutoID disabled, use KeyType.UUID32 or KeyType.UUID36 in the entity mapping. dbVisitor generates and assigns the value before INSERT. Reserve at least 32 or 36 characters in the field; see Key Generators.
Choosing the key source
For method annotations and Mapper files, use useGeneratedKeys with the default key source. The generated-key result is separate from the statement's ordinary result set; do not set generatedKeySource="resultSet" for these inserts.
The examples above show the supported generated-key path, including how to select the returned column with keyColumn.
Key Strategies in Mapper Files
Bind an assigned key in INSERT, or use useGeneratedKeys to receive an AutoID. Do not use generatedKeySource="resultSet": INSERT does not return an ordinary result set for filling keys.
Milvus has no database sequences, so selectKey cannot query a sequence value to generate a key. Assign the ID in the application when it must be known in advance. AutoID usage is shown above.