title: MySQL After Insert Trigger to table with FK to first table
body: <p>This is my current Trigger:</p>
  
  <pre><code>CREATE DEFINER=`root`@`localhost` TRIGGER `setaccessrole` AFTER INSERT ON `user` FOR EACH ROW BEGIN
      INSERT INTO user_role_linker (user_id, role_id) values (last_insert_id(), 2);
    END
  </code></pre>
  
  <p>user_role_linker.user_id is FK to user.id, I want an insert into user_role_linker including the last inserted autoincremented ID + user_role = 2, but I get:</p>
  
  <pre><code>#1452 - Cannot add or update a child row: a foreign key constraint fails (`database`.`user_role_linker`, CONSTRAINT `FK_61117899A76ED395` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`))
  </code></pre>
  
  <p>edit:
  I had to use "NEW.id" instead of last_insert_id().
  This works.</p>