Create table fails with Foreign Key Constraint is incorrectly Formed Create table fails with Foreign Key Constraint is incorrectly Formed database database

Create table fails with Foreign Key Constraint is incorrectly Formed


I have received this message many times while using 3rd party tools to create tables and then constrain against existing tables. It's either one of two things:

  • The int columns have different sizes
  • The int columns have different flags (sans AUTO_INCREMENT)

As an example, I created a table with a tool that somehow created a column as INT(10) instead of the expected INT(11). Even though I just chose INT when creating both, it was messed up - never tracked down why.

Long story short, it's generally best to explicitly state the INT size when creating a table.

In your case, the following should work:

create table users (id int(11) not null auto_increment, username varchar(255) NOT NULL, password varchar(255) NOT NULL, active int NOT NULL, PRIMARY KEY (id))ENGINE=InnoDB COLLATE=utf8_unicode_ci;create table athing (id int(11) not null auto_increment, name varchar(255) not null, status varchar(255) not null, created_by_user_id int(11) not null, PRIMARY KEY (id), CONSTRAINT athing_fk1 FOREIGN KEY (created_by_user_id) REFERENCES users (id)) ENGINE=InnoDB COLLATE=utf8_unicode_ci;