Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello everyone, I got this error
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]
while I was trying to develop a CRUD Restful API Application using Spring Boot, Angular and MySQL database. I tried to insert a record into the database and tested it with Postman to see if it will work and that error came up in Postman. The id and tools_id fields are set to AUTO_INCREMENT, NOT NULL in MySQL DDL statement that I used to create the table named "Tools", so those fields ought to Auto increase by themselves when the new record is created in the database.

There are no errors in the Spring boot classes, packages and in the Angular as well.

What I have tried:

This is my Entity class in Spring boot --> Tools.java
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import lombok.Getter;
import lombok.Setter;

@Entity
@Getter
@Setter
public class Tools {
	
	@Id
	@GeneratedValue(strategy= GenerationType.IDENTITY)
	private Long id;
	
	private Long tool_id;
	
	private String tool_name;
	
	private Long tag_id;
	
	private String note_1;
	
	private String note_2;
	
	private String note_3;
		
}


This is my Controller class

@CrossOrigin("*")
@RestController
@RequestMapping("/api/v1")
public class ToolsController {

	@Autowired
	ToolsService toolsService;
	
	
	@GetMapping("/tools")
	public ResponseEntity<List<Tools>> get() {
		List<Tools> tool_s = toolsService.findAll();
		return new ResponseEntity<List<Tools>>(tool_s, HttpStatus.OK);
	}
	
	@PostMapping("/tools")
	public ResponseEntity<Tools> save(@RequestBody Tools tools) {
		 Tools toolOne = toolsService.save(tools);
		return new ResponseEntity<Tools>(toolOne, HttpStatus.OK);
	}
	
}


This is what I put in Postman for the new database record.

{
	"tool_name": "wheel barrow",
	"tool_tag": 111103,
	"note_1": "This is a new note for one",
	"note_2": "Note two of note 2",
	"note_3": "Another note for note three"
}


This is the DDL statement from Workbench which I used in creating the table "Tools", the name of the database is flex.

CREATE TABLE IF NOT EXISTS flex.tools (
	id BIGINT(11) NOT NULL AUTO_INCREMENT,
    tool_id BIGINT(11) NOT NULL,
    tool_name VARCHAR(50) NOT NULL,
	tag_id BIGINT(11) NOT NULL,
    note_1 LONGTEXT NULL,
    note_2 LONGTEXT NULL,
    note_3 LONGTEXT NULL,
    PRIMARY KEY (id),
    INDEX idx_tool_id (tool_id),
    INDEX idx_tag_id (tag_id),
    INDEX idx_tool_name (tool_name)
    
);


I think that Postman is pointing out that either or both id and tools_id fields cannot be NULL but it's meant to AUTO increase. Please how can I solve this problem ? Thanks for any help.
Posted
Updated 11-Apr-22 14:55pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900