Determine cause of SQLConstraintViolationException

Refresh

April 2019

Views

50 time

1

I am currently writing some code that works on updating values in a database. I have a table with three constrains: a primary key (composite), a foreign key (not composite), and a unique key (composite).

My table (the output of show create table ItemAttributeMaster):

CREATE TABLE `ItemAttributeMaster` (
  `SKU` int(11) NOT NULL,
  `ItemAttributeID` int(11) NOT NULL,
  `Color` varchar(45) DEFAULT NULL,
  `Size` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`SKU`,`ItemAttributeID`),
  UNIQUE KEY `ItemAttribute_Unique` (`SKU`,`Color`,`Size`),
  CONSTRAINT `ItemAttribute_SKU` FOREIGN KEY (`SKU`) REFERENCES `ItemMaster` (`SKU`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1

I have a trigger (this question) that makes the ItemAttributeID auto-increment as if it was an MyISAM table, so the primary key shouldn't be violated

My code has the possibility of throwing an SQLIntegrityConstraintViolationException. In that past, when I have had only one possible constraint violation exception (the foreign key), I assumed the SQLIntegrityConstraintViolationException came from that constraint. But in this table, where I have two possible constraints that could be violated (the foreign key and the unique key), how can I tell which of these the SQLIntegrityConstraintViolationException is referring to? Can I get the constraint type? Constraint name? Field names? I have scoured the web and JavaDoc, and can not find this.

0 answers