Search code examples
mysqlinsertforeign-keysinnodbmysql-error-1054

Can´t INSERT values with Foreign Key in MySQL


I have a MySQL (5.1.42 on OsX) running. I added a Foreign Key with this sql statement:

ALTER TABLE `Portal`.`Mitarbeiter_2_BlackBerry` 
  ADD CONSTRAINT `fk_Blackberry`
  FOREIGN KEY (`id` )
  REFERENCES `Portal`.`Blackberry` (`id` )
  ON DELETE NO ACTION
  ON UPDATE NO ACTION
, ADD INDEX `fk_Blackberry` (`id` ASC)

But when i try to insert values in that table with this sql statement:

INSERT INTO Mitarbeiter_2_BlackBerry SET uebergabeAm = '2009-12-01 13:00:00', fk_Blackberry = (SELECT id FROM Blackberry WHERE id = '1')

I got the following Error: Error Code: 1054 Unknown column 'fk_BlackBerry' in 'field list'

Anybody an idea what could be wrong? Thanks for any hint :-) Lars.


Solution

  • You need to put the value in the column id not the constraint fk_Blackerry. And if you know the value is 1, just insert 1- you don't need the subquery.