0

Let us assume that we have a products table and a orders table. The products table have a primary key (tinyint) and the orders table have a primary key (int).

Try adding the foreign key:

alter table `products_orders`
add constraint `products_orders_products_id_foreign` foreign key (`products_id`)
references `products` (`id`) on delete cascade;

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
Thomas Müller
  • 420
  • 4
  • 15
  • You should include the create table statements, or meaningful abbreviations of them, for both the `products_orders` and `products` tables. This error could be caused by a handful of things. – Tim Biegeleisen Aug 18 '18 at 16:40

1 Answers1

1

Foreign key reference columns data types must be exactly the same (including not null and unsigned flags etc). This avoids any ambiguity when it comes to comparison.

I would recommend that any foreign key columns should be type Int and unsigned.

Simon R
  • 3,732
  • 4
  • 31
  • 39