title: MySQL Error 1452 - can't insert data
body: <p>I am inserting some data into the following MySQL tables:</p>
  
  <pre><code>CREATE TABLE genotype
  (
  Genotype VARCHAR(20),
  Fitness FLOAT NULL,
  Tally INT NULL,
  PRIMARY KEY (Genotype)
  )ENGINE=InnoDB;
  
  
  CREATE TABLE gene
  (
  Gene VARCHAR(20),
  E FLOAT NOT NULL,
  Q2 FLOAT NOT NULL, 
  PRIMARY KEY (Gene)
  )ENGINE=InnoDB;
  
  CREATE TABLE genotypegene
  (
  Genotype VARCHAR(20),
  Gene VARCHAR(20),
  FOREIGN KEY (Genotype) REFERENCES genotype(Genotype),
  FOREIGN KEY (Gene) REFERENCES gene(Gene)
  )ENGINE=InnoDB;
  </code></pre>
  
  <p>I inserted the data into genotype/gene first, but get the following error when trying to insert into genotypegene:</p>
  
  <pre><code>ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`populationdb`.`genotypegene`, CONSTRAINT `genotypegene_ibfk_2` FOREIGN KEY (`Gene`) REFERENCES `gene` (`Gene`))
  </code></pre>
  
  <p>The data I'm inserting is into this table is:
  Genotype1,Gene1
  Genotype1,Gene2
  Genotype1,Gene3
  Genotype1,Gene4</p>
  
  <p>There is one copy of Genotype1 in the genotype table, the idea being that each genotype can contain many genes, but each gene can exist in multiple genotypes (at the moment, there is only 1 genotype, but later I will insert more). I read <a href="https://stackoverflow.com/questions/8306585/foreign-key-constraint-error-1452-in-mysql">here</a> that I could turn off Foreign Key Checks, but I am reluctant to do so without knowing the reason for this error. Is this because there is only one copy of Genotype1 in the genotype table? (I have checked that Genotype1, Gene1 etc. are in the same format/spelling in their primary key tables).</p>
  
  <p>Just in case, here is the code I am using to insert the data:</p>
  
  <pre><code> mysql&gt; LOAD DATA LOCAL INFILE 'C:\\.....genotypegene.csv'
     -&gt; INTO TABLE genotypegene
     -&gt; FIELDS TERMINATED BY ','
     -&gt; (Genotype, Gene);
  </code></pre>
  
  <p>Thanks</p>