问题
I have a big problem
my unitils.properties:
database.driverClassName=com.mysql.jdbc.Driver
database.url=jdbc:mysql://localhost/timesheet_test
database.userName=root
database.password=
database.schemaNames=timesheet_test
database.dialect=mysql
updateDataBaseSchema.enabled=true
dbMaintainer.script.locations=src/test/resources/dbscripts
dataSetStructureGenerator.xsd.dirName=target/xsd
dbMaintainer.autoCreateExecutedScriptsTable=true
dbMaintainer.disableConstraints.enabled=false
## Módulos do unitils
unitils.module.database.enabled=true
unitils.module.dbunit.enabled=true
unitils.module.hibernate.enabled=true
unitils.module.spring.enabled=true
unitils.module.mock.enabled=false
unitils.module.easymock.enabled=false
unitils.module.inject.enabled=false
unitils.module.jpa.enabled=false
DatabaseModule.Transactional.value.default=rollback
my database.sql:
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
CREATE SCHEMA IF NOT EXISTS `timesheet_test` DEFAULT CHARACTER SET latin1 ;
USE `timesheet_test` ;
-- -----------------------------------------------------
-- Table `timesheet_test`.`DATA`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `timesheet_test`.`DATA` (
`ID_DATA` BIGINT(20) NOT NULL AUTO_INCREMENT ,
`DATA` DATETIME NOT NULL ,
PRIMARY KEY (`ID_DATA`) ,
UNIQUE INDEX `data_UNIQUE` (`DATA` ASC) ,
UNIQUE INDEX `ID_DIAS_UNIQUE` (`ID_DATA` ASC) )
ENGINE = InnoDB
AUTO_INCREMENT = 1000
DEFAULT CHARACTER SET = latin1;
-- -----------------------------------------------------
-- Table `timesheet_test`.`USUARIO`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `timesheet_test`.`USUARIO` (
`ID_USUARIO` BIGINT(20) NOT NULL AUTO_INCREMENT ,
`E_MAIL` VARCHAR(100) NOT NULL ,
`SENHA` VARCHAR(10) NOT NULL ,
`NOME` VARCHAR(100) NULL DEFAULT NULL ,
PRIMARY KEY (`ID_USUARIO`) ,
UNIQUE INDEX `E_MAIL_UNIQUE` (`E_MAIL` ASC) ,
UNIQUE INDEX `ID_USUARIO_UNIQUE` (`ID_USUARIO` ASC) )
ENGINE = InnoDB
AUTO_INCREMENT = 1000
DEFAULT CHARACTER SET = latin1;
-- -----------------------------------------------------
-- Table `timesheet_test`.`TIME_SHEET`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `timesheet_test`.`TIME_SHEET` (
`ID_TIME_SHEET` BIGINT(20) NOT NULL AUTO_INCREMENT ,
`INICIO` VARCHAR(8) NOT NULL ,
`FIM` VARCHAR(8) NOT NULL ,
`LONGITUDE` DOUBLE NULL DEFAULT NULL ,
`LATITUDE` DOUBLE NULL DEFAULT NULL ,
`OBSERVACAO` VARCHAR(200) NULL DEFAULT NULL ,
`ID_DATA` BIGINT(20) NOT NULL ,
`ID_USUARIO` BIGINT(20) NOT NULL ,
PRIMARY KEY (`ID_TIME_SHEET`) ,
UNIQUE INDEX `ID_TIME_SHEET_UNIQUE` (`ID_TIME_SHEET` ASC) ,
INDEX `FK9735054D39B29F66` (`ID_DATA` ASC) ,
INDEX `FK9735054D9DA5550A` (`ID_USUARIO` ASC) ,
CONSTRAINT `FK9735054D9DA5550A`
FOREIGN KEY (`ID_USUARIO` )
REFERENCES `timesheet_test`.`USUARIO` (`ID_USUARIO` ),
CONSTRAINT `FK9735054D39B29F66`
FOREIGN KEY (`ID_DATA` )
REFERENCES `timesheet_test`.`DATA` (`ID_DATA` ))
ENGINE = InnoDB
AUTO_INCREMENT = 1000
DEFAULT CHARACTER SET = latin1;
USE `timesheet_test` ;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
I have 65 tests and I have problem with 6 and the same error:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`timesheet_test`.`time_sheet`, CONSTRAINT `FK9735054D39B29F66` FOREIGN KEY (`ID_DATA`) REFERENCES `DATA` (`ID_DATA`))
My test is "search" not "delete" but I think when unitils trying delete row to start new test, have problem.
anybody have idea ?
thanks
回答1:
Solution can be found on: https://sourceforge.net/p/unitils/discussion/570578/thread/38fe5abc/
During a clean-insert, data is first deleted from all tables in reverse order as specified in the data set. After that, the data is inserted in the specified order. So for your data set
it first deletes all records in usuario then data and then time_sheet since there appears to be FK from time_sheet to data (see exception), it cannot delete the data records before deleting the dependent time_sheet records Solution: just correct the order in the data set (put data before time sheet)
<TIME_SHEET /> <DATA /> <USUARIO />
<DATA /> <TIME_SHEET /> <USUARIO />
来源:https://stackoverflow.com/questions/17204060/unitils-doesnt-work