Question

I wrote a script in Perl that creates a database schema:

CREATE TABLE descType (
    id MEDIUMINT unsigned PRIMARY KEY,
    descr MEDIUMTEXT)ENGINE=InnoDB;

CREATE TABLE taxType (
    id MEDIUMINT unsigned PRIMARY KEY,
    descr TEXT not null)ENGINE=InnoDB;

CREATE TABLE uniref(
            id INT unsigned PRIMARY KEY,
            seqId varchar (50) not null,
            descId MEDIUMINT unsigned not null,
            n MEDIUMINT unsigned not null,
            taxId MEDIUMINT  unsigned not null,
            repId varchar (50) not null,
            foreign KEY (descId) REFERENCES descType(id),
            FOREIGN KEY (taxId) REFERENCES taxType(id),
            unique(seqId)
            )ENGINE=InnoDB; 

When I use this command:

system qq(mysqlimport -u$mySqlUser -p$mySqlPass $database $table --local --fields-terminated-by="\t" --lines-terminated-by="\r\n") )== 0
   or die "ERROR: an error occurred while importing $table in $database. $?";

I get this error:

mysqlimport: Error: 1452, Cannot add or update a child row: a foreign key 
constraint fails (`uniref_2013_08`.`uniref`, CONSTRAINT `uniref_ibfk_1` 
FOREIGN KEY (`descId`) REFERENCES `descType` (`id`)), when using 
table: uniref

I can't figure out what I am doing wrong. I was using the same script on another machine and it was working fine.

Was it helpful?

Solution

Foreign key preserves data integrity and prevents insertion if there is no parent row. You need to populate descType parent table first.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top