Create table fails with Foreign Key Constraint is incorrectly Formed

后端 未结 1 631
太阳男子
太阳男子 2021-01-12 00:34

Topic

MariaDB InnoDB Foreign Key Issue

Want to start off by saying I\'m new to InnoDB and spent all day reading posts yesterday I\'ve tried multiple things

相关标签:
1条回答
  • 2021-01-12 01:05

    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;
    
    0 讨论(0)
提交回复
热议问题