title: MySQL error: Cannot add foreign key constraint (PrestaShop module)
body: <p>I'm trying to write MySQL queries for my custom PrestaShop module. I encountered an issue while adding foreign keys and have no idea what might be wrong. I double checked column types and their existance.</p>
  <p>Here's the error:</p>
  <blockquote>
  <p>Cannot add foreign key constraint</p>
  </blockquote>
  <pre><code>CREATE TABLE IF NOT EXISTS `ps_pp_project` (
      `id` INT NOT NULL AUTO_INCREMENT,
      `id_group` INT,
      `id_product` INT,
      `name` VARCHAR(255),
      `height` INT,
      `width` INT,
      `file` VARCHAR(255),
      `class_name` VARCHAR(255),
      PRIMARY KEY (`id`),
      FOREIGN KEY (`id_product`) REFERENCES `' ._DB_PREFIX_. '
      product`(`id_product`) ON DELETE CASCADE,
      FOREIGN KEY (`id_group`) REFERENCES `' ._DB_PREFIX_. 
      'pp_group`(`id`) ON DELETE CASCADE
  ) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=UTF8mb4';
  </code></pre>
  <p>And here is all the code I'm trying to run during installation</p>
  <pre><code>    $sqls[] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'pp_group` (
      `id` INT NOT NULL AUTO_INCREMENT,
      `name` VARCHAR(255) NULL,
      PRIMARY KEY (`id`)
  ) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=UTF8mb4';
  
  $sqls[] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'pp_project` (
      `id` INT NOT NULL AUTO_INCREMENT,
      `id_group` INT,
      `id_product` INT(10),
      `name` VARCHAR(255),
      `height` INT,
      `width` INT,
      `file` VARCHAR(255),
      `class_name` VARCHAR(255),
      PRIMARY KEY (`id`),
      FOREIGN KEY (`id_product`) REFERENCES `' ._DB_PREFIX_. 'product`(`id_product`)
      ON DELETE CASCADE,
      FOREIGN KEY (`id_group`) REFERENCES `' ._DB_PREFIX_. 'pp_group`(`id`)
      ON DELETE CASCADE
  ) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=UTF8mb4';
  
  $sqls[] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'pp_layer` (
      `id` INT NOT NULL AUTO_INCREMENT,
      `id_project` INT,
      `label` VARCHAR(255) DEFAULT \'Twój tekst\',
      `position_x` INT DEFAULT 0,
      `position_y` INT DEFAULT 0,
      `max_chars` INT DEFAULT 10,
      `text_align` VARCHAR(255) DEFAULT \'left\',
      `height` INT DEFAULT 0,
      `width` INT DEFAULT 0,
      `placeholder` VARCHAR(255) DEFAULT \'Tekst\',
      `rotation` INT DEFAULT 0,
      `skew_x` INT DEFAULT 0,
      `skew_y` INT DEFAULT 0,
      `color` VARCHAR(255) DEFAULT \'000\',
      `font_family` VARCHAR(255) DEFAULT \'arial\',
      `font_size` INT DEFAULT 12,
      `line_height` INT DEFAULT 12,
      `row_quantity` INT DEFAULT 1,
      `visible` BOOL DEFAULT 1,
      `layer_type` VARCHAR(255) DEFAULT \'text\',
      `file` VARCHAR(255) DEFAULT null,
      `opacity` FLOAT DEFAULT 1.0,
      PRIMARY KEY (`id`),
      FOREIGN KEY (`id_project`) REFERENCES `' ._DB_PREFIX_. 'pp_project`(`id`)
      ON DELETE CASCADE
  ) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=UTF8mb4';
  
  
  foreach ($sqls as $sql) {
      if (!Db::getInstance()-&gt;execute($sql)) {
          return false;
      }
  }
  </code></pre>
  <p>The queries execute perfectly without foreign keys.</p>
  <blockquote>
  <p>UPDATE</p>
  </blockquote>
  <p>Found out that the issue is this line:</p>
  <pre><code>FOREIGN KEY (`id_product`) REFERENCES `' ._DB_PREFIX_. '
  product`(`id_product`) ON DELETE CASCADE,
  </code></pre>
  <p>However, I haven't yet figured out how to fix it.</p>