Skip to content

Commit 9cf5537

Browse files
authored
feat: update insert logic to consider allow_insert_auto_id property (#494)
* fix(grpc): update insert logic to consider allow_insert_auto_id property - Add retrieval of allow_insert_auto_id from collection properties - Modify insertable logic to include allow_insert_auto_id condition - Enhance Insert API tests to validate behavior with allow_insert_auto_id property Signed-off-by: ryjiang <jiangruiyi@gmail.com> * fix test Signed-off-by: ryjiang <jiangruiyi@gmail.com> --------- Signed-off-by: ryjiang <jiangruiyi@gmail.com>
1 parent 329795f commit 9cf5537

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed

milvus/grpc/Data.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,17 @@ export class Data extends Collection {
145145
throw collectionInfo;
146146
}
147147

148+
// get property allow_insert_auto_id
149+
const allowInsertAutoId = collectionInfo.properties.find(
150+
p => p.key === 'allow_insert_auto_id'
151+
)?.value;
152+
148153
// Tip: The field data sequence needs to be set same as `collectionInfo.schema.fields`.
149154
const functionOutputFields: string[] = [];
150155
const fieldMap = new Map<string, _Field>(
151156
collectionInfo.schema.fields.reduce((acc, v) => {
152-
// if autoID is true, ignore the primary key field or if upsert is true
153-
const insertable = !v.autoID || upsert;
157+
// if autoID is true, ignore the primary key field or if upsert is true or allow_insert_auto_id is true
158+
const insertable = !v.autoID || upsert || allowInsertAutoId === 'true';
154159

155160
// if function field is set, you need to ignore the field value in the data.
156161
if (v.is_function_output) {

test/grpc/Insert.spec.ts

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,12 @@ describe(`Insert API`, () => {
252252
},
253253
],
254254
},
255+
properties: [
256+
{
257+
key: 'allow_insert_auto_id',
258+
value: 'false',
259+
},
260+
],
255261
} as any);
256262
});
257263
};
@@ -271,9 +277,7 @@ describe(`Insert API`, () => {
271277
await fakeClient.insert(params);
272278
expect('a').toEqual('b');
273279
} catch (error) {
274-
expect(error.message).toContain(
275-
ERROR_REASONS.FIELD_TYPE_IS_NOT_SUPPORT
276-
);
280+
expect(error.message).toContain(ERROR_REASONS.FIELD_TYPE_IS_NOT_SUPPORT);
277281
} finally {
278282
fakeClient.closeConnection();
279283
}
@@ -496,4 +500,34 @@ describe(`Insert API`, () => {
496500
expect(error.message).toEqual(ERROR_REASONS.INSERT_CHECK_WRONG_DIM);
497501
}
498502
});
503+
504+
it(`Insert data on autoId field after change allow_insert_auto_id property should success`, async () => {
505+
// before should throw error
506+
try {
507+
await milvusClient.insert({
508+
collection_name: COLLECTION_NAME,
509+
fields_data: [{ a: 1 }],
510+
});
511+
expect('a').toEqual('b');
512+
} catch (error) {
513+
// should throw error
514+
expect(error.message).toContain('Insert fail');
515+
}
516+
await milvusClient.alterCollectionProperties({
517+
collection_name: COLLECTION_NAME,
518+
properties: {
519+
allow_insert_auto_id: 'true',
520+
},
521+
});
522+
523+
const dataToInsert = generateInsertData(COLLECTION_NAME_PARAMS.fields, 2);
524+
const params: InsertReq = {
525+
collection_name: COLLECTION_NAME,
526+
partition_name: PARTITION_NAME,
527+
fields_data: dataToInsert,
528+
};
529+
530+
const res = await milvusClient.insert(params);
531+
expect(res.status.error_code).toEqual(ErrorCode.SUCCESS);
532+
});
499533
});

test/grpc/Upsert.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,12 @@ describe(`Upsert API`, () => {
377377
},
378378
],
379379
},
380+
properties: [
381+
{
382+
key: 'allow_insert_auto_id',
383+
value: 'false',
384+
},
385+
],
380386
} as any);
381387
});
382388
};

0 commit comments

Comments
 (0)