how to fix errno: 150 of MySQL “Foreign key constraint is incorrectly formed” [closed]

I am using WordPress, and when attempting to create a table with the following SQL statement:

CREATE TABLE stars (
    ID INT AUTO_INCREMENT PRIMARY KEY,
    Star INT CHECK (Star >= 1 AND Star <= 5),
    ID_comment BIGINT(20),
    FOREIGN KEY (ID_comment) REFERENCES wp_comments(comment_ID)
);

table “wp-comments”:
enter image description here

ERROR:enter image description here

I encountered MySQL error 150.”

How to fix error, answer

  • 1

    Please add the error message in full and the table defintion of wp_comments

    – 

  • wp_comments is a table created by WordPress, so I don’t have its exact definition

    – 

  • 1

    Please add the error message in full

    – 

  • 1

    I don't have its exact definition…you can easily look it up – something like mysql workbench or phpmyadmin will generate a CREATE TABLE statement for you

    – 

  • 1

    comment_id in your code needs to be the same as in wp_comments and should be unsigned,, ‘The size and sign of fixed precision types such as INTEGER and DECIMAL must be the same.’ – dev.mysql.com/doc/refman/8.0/en/create-table-foreign-keys.html

    – 




Leave a Comment