Primary key
Introduction
Verify that the request parameters are unique. For example, the product number in the product table is unique, and the email address in the user table is unique.
Example
Prepare data table
Assume that there is hrm.salaries human resources system - employee salary information table, with the following structure:
--Salary table
CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`),
CONSTRAINT `salaries_ibfk_1` FOREIGN KEY (`emp_no`) REFERENCES `employees` (`emp_no`) ON DELETE CASCADE
)
You can see from the above table structure:
- The primary key is the combination of "employee number" emp_no and "start date" from_date, which is used to uniquely identify a salary record. The value must be unique
- The employee number field emp_no is used as a foreign key. For foreign key constraints, please refer to Foreign Key
Example 1: Normal request
POST http://127.0.0.1:21000
Content-Type: application/json;charset=utf-8
{
"jsonrpc": "2.0",
"method": "hrm.salaries.add",
"params": {
"empNo": 10010,
"salary": 10000,
"fromDate": "2023-10-01",
"toDate": "2023-10-31"
},
"id": "client-unique-request-id"
}
Response
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
{
"jsonrpc": "2.0",
"result": {
"empNo": 10010,
"fromDate": "2023-10-01"
},
"id": "client-unique-request-id"
}
Example 2: Bad request
Submit the request repeatedly, and it is detected that the combination of "employee number" and "start date" already exists, and the response is incorrect.
POST http://127.0.0.1:21000
Content-Type: application/json;charset=utf-8
{
"jsonrpc": "2.0",
"method": "hrm.salaries.add",
"params": {
"empNo": 10010,
"salary": 10000,
"fromDate": "2023-10-01",
"toDate": "2023-10-31"
},
"id": "client-unique-request-id"
}
Response error
HTTP/1.1 400 Bad Request
Content-Type: application/json; charset=utf-8
{
"jsonrpc": "2.0",
"error": {
"code": -32600,
"message": "checking for primary key conflicts: duplicate primary key in hrm.salaries [empNo fromDate]",
"data": {
"errorCode": "EU.Constraint.PK.Duplicated",
"errorDetails": [
{
"constraint": "PrimaryKeyDuplicated",
"field": [
"empNo",
"fromDate"
],
"fieldValue": [
10010,
"2023-10-01"
],
"location": "hrm.salaries",
"message": "duplicate primary key in hrm.salaries.[empNo fromDate] [10010 2023-10-01]"
}
],
"errorMessage": "duplicate primary key in hrm.salaries [empNo fromDate]"
}
},
"id": "client-unique-request-id"
}
Tested on Luwak 1.13.0