diff --git a/meilisearch-http/src/routes/document.rs b/meilisearch-http/src/routes/document.rs index a3e59ca05..3669e3899 100644 --- a/meilisearch-http/src/routes/document.rs +++ b/meilisearch-http/src/routes/document.rs @@ -145,7 +145,7 @@ async fn update_multiple_documents(mut ctx: Request, is_partial: bool) -> Some(id) => id, None => match data.first().and_then(|docs| find_primary_key(docs)) { Some(id) => id, - None => return Err(ResponseError::bad_request("Could not infer a schema")), + None => return Err(ResponseError::bad_request("Could not infer a primary key")), }, }; diff --git a/meilisearch-http/tests/common.rs b/meilisearch-http/tests/common.rs index eee0fc283..3814db6de 100644 --- a/meilisearch-http/tests/common.rs +++ b/meilisearch-http/tests/common.rs @@ -205,6 +205,11 @@ impl Server { self.post_request_async(&url, body); } + pub fn add_or_replace_multiple_documents_sync(&mut self, body: Value) -> (Value, StatusCode) { + let url = format!("/indexes/{}/documents", self.uid); + self.post_request(&url, body) + } + pub fn add_or_update_multiple_documents(&mut self, body: Value) { let url = format!("/indexes/{}/documents", self.uid); self.put_request_async(&url, body); diff --git a/meilisearch-http/tests/index.rs b/meilisearch-http/tests/index.rs index 849cc425d..a16c10575 100644 --- a/meilisearch-http/tests/index.rs +++ b/meilisearch-http/tests/index.rs @@ -625,3 +625,36 @@ fn create_index_without_primary_key_and_search() { assert_eq!(status_code, 200); assert_eq!(response["hits"].as_array().unwrap().len(), 0); } + +// Test the error message when we push an document update and impossibility to find primary key +// Test issue https://github.com/meilisearch/MeiliSearch/issues/517 +#[test] +fn check_add_documents_without_primary_key() { + let mut server = common::Server::with_uid("movies"); + + // 1 - Create the index with no primary_key + + let body = json!({ + "uid": "movies", + }); + let (response, status_code) = server.create_index(body); + assert_eq!(status_code, 201); + assert_eq!(response["primaryKey"], json!(null)); + + // 2- Add document + + let body = json!([{ + "title": "Test", + "comment": "comment test" + }]); + + let (response, status_code) = server.add_or_replace_multiple_documents_sync(body); + + let expected = json!({ + "message": "Could not infer a primary key" + }); + + assert_eq!(status_code, 400); + assert_json_eq!(response, expected, ordered: false); +} +