title: MariaDB: Can't create table, errno: 150 "Foreign key constraint is incorrectly formed")
body: <p>I've been trying to build a database for a side-project of mine, but I can't seem to fathom the notion of foreign keys creation. Here is my code (I would be more than grateful if you could take a look):</p>
  
  <pre><code>CREATE TABLE IF NOT EXISTS meeting (
  meeting_id SERIAL PRIMARY KEY,
  period_no varchar(10) NOT NULL,
  session_no varchar(10) NOT NULL,
  meeting_no varchar(10) NOT NULL,
  date DATE NOT NULL,
  chair_id BIGINT UNSIGNED,
  agenda_id BIGINT UNSIGNED,
  CONSTRAINT FOREIGN KEY (chair_id) REFERENCES mp (mp_id),
  CONSTRAINT FOREIGN KEY (agenda_id) REFERENCES agenda (agenda_id)
  )engine=innodb;
  
  CREATE TABLE IF NOT EXISTS agenda (
  agenda_id SERIAL PRIMARY KEY,
  name varchar(100) NOT NULL,
  special boolean NOT NULL DEFAULT 0,
  meeting_id BIGINT UNSIGNED,
  speech_id BIGINT UNSIGNED,
  FOREIGN KEY (meeting_id) REFERENCES meeting(meeting_id),
  FOREIGN KEY (speech_id) REFERENCES agenda(speech)
  )engine=innodb;
  
  CREATE TABLE IF NOT EXISTS speech (
  speech_id SERIAL PRIMARY KEY,
  body text, 
  mp_id BIGINT UNSIGNED,
  FOREIGN KEY (chair_id) REFERENCES mp(mp_id)
  )engine=innodb;
  
  CREATE TABLE IF NOT EXISTS mp (
  mp_id SERIAL PRIMARY KEY,
  name varchar(100) NOT NULL,
  chair_in_meeting_id BIGINT UNSIGNED NULL,
  speech_id BIGINT UNSIGNED,
  FOREIGN KEY (chair_in_meeting_id) REFERENCES meeting(meeting_id),
  FOREIGN KEY (speech_id) REFERENCES agenda(speech)
  )engine=innodb;
  </code></pre>
  
  <p>The process stops at the first table. I thought there should be a specific order of creating the tables with their primary keys and then alter them by adding the reference for the foreign keys, but that didn't work either. Any ideas?</p>
  
  <p>MariaDB version is: 10.0.29-MariaDB SLE 12 SP1 package</p>
  
  <p>EDIT:</p>
  
  <blockquote>
    <hr>
    
    <h2>LATEST FOREIGN KEY ERROR</h2>
    
    <p>2017-03-10 14:55:03 7f3180ae5700 Error in foreign key constraint of table <code>politikoslogos</code>.<code>IF</code>:
    Create  table <code>politikoslogos</code>.<code>IF</code> with foreign key constraint failed. Referenced table <code>politikoslogos</code>.<code>mp</code> not found in the data dictionary near 'FOREIGN KEY (chair_id) REFERENCES mp (mp_id),
        CONSTRAINT FOREIGN KEY (agenda_id) REFERENCES agenda (agenda_id)
    )engine=innodb'.</p>
  </blockquote>