MySQL Creating tables with Foreign Keys giving errno: 150











up vote
94
down vote

favorite
44












I am trying to create a table in MySQL with two foreign keys, which reference the primary keys in 2 other tables, but I am getting an errno: 150 error and it will not create the table.



Here is the SQL for all 3 tables:



CREATE TABLE role_groups (
`role_group_id` int(11) NOT NULL `AUTO_INCREMENT`,
`name` varchar(20),
`description` varchar(200),
PRIMARY KEY (`role_group_id`)
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS `roles` (
`role_id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50),
`description` varchar(200),
PRIMARY KEY (`role_id`)
) ENGINE=InnoDB;

create table role_map (
`role_map_id` int not null `auto_increment`,
`role_id` int not null,
`role_group_id` int not null,
primary key(`role_map_id`),
foreign key(`role_id`) references roles(`role_id`),
foreign key(`role_group_id`) references role_groups(`role_group_id`)
) engine=InnoDB;


Any help would be greatly appreciated.










share|improve this question




















  • 1




    Could you post the error output and tell us which command (of the three) is causing the error?
    – dave
    Sep 21 '09 at 23:08






  • 4




    What's with the back-ticks around auto_increment? That's not valid. Auto_increment is a keyword, not an identifier.
    – Bill Karwin
    Sep 21 '09 at 23:39















up vote
94
down vote

favorite
44












I am trying to create a table in MySQL with two foreign keys, which reference the primary keys in 2 other tables, but I am getting an errno: 150 error and it will not create the table.



Here is the SQL for all 3 tables:



CREATE TABLE role_groups (
`role_group_id` int(11) NOT NULL `AUTO_INCREMENT`,
`name` varchar(20),
`description` varchar(200),
PRIMARY KEY (`role_group_id`)
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS `roles` (
`role_id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50),
`description` varchar(200),
PRIMARY KEY (`role_id`)
) ENGINE=InnoDB;

create table role_map (
`role_map_id` int not null `auto_increment`,
`role_id` int not null,
`role_group_id` int not null,
primary key(`role_map_id`),
foreign key(`role_id`) references roles(`role_id`),
foreign key(`role_group_id`) references role_groups(`role_group_id`)
) engine=InnoDB;


Any help would be greatly appreciated.










share|improve this question




















  • 1




    Could you post the error output and tell us which command (of the three) is causing the error?
    – dave
    Sep 21 '09 at 23:08






  • 4




    What's with the back-ticks around auto_increment? That's not valid. Auto_increment is a keyword, not an identifier.
    – Bill Karwin
    Sep 21 '09 at 23:39













up vote
94
down vote

favorite
44









up vote
94
down vote

favorite
44






44





I am trying to create a table in MySQL with two foreign keys, which reference the primary keys in 2 other tables, but I am getting an errno: 150 error and it will not create the table.



Here is the SQL for all 3 tables:



CREATE TABLE role_groups (
`role_group_id` int(11) NOT NULL `AUTO_INCREMENT`,
`name` varchar(20),
`description` varchar(200),
PRIMARY KEY (`role_group_id`)
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS `roles` (
`role_id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50),
`description` varchar(200),
PRIMARY KEY (`role_id`)
) ENGINE=InnoDB;

create table role_map (
`role_map_id` int not null `auto_increment`,
`role_id` int not null,
`role_group_id` int not null,
primary key(`role_map_id`),
foreign key(`role_id`) references roles(`role_id`),
foreign key(`role_group_id`) references role_groups(`role_group_id`)
) engine=InnoDB;


Any help would be greatly appreciated.










share|improve this question















I am trying to create a table in MySQL with two foreign keys, which reference the primary keys in 2 other tables, but I am getting an errno: 150 error and it will not create the table.



Here is the SQL for all 3 tables:



CREATE TABLE role_groups (
`role_group_id` int(11) NOT NULL `AUTO_INCREMENT`,
`name` varchar(20),
`description` varchar(200),
PRIMARY KEY (`role_group_id`)
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS `roles` (
`role_id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50),
`description` varchar(200),
PRIMARY KEY (`role_id`)
) ENGINE=InnoDB;

create table role_map (
`role_map_id` int not null `auto_increment`,
`role_id` int not null,
`role_group_id` int not null,
primary key(`role_map_id`),
foreign key(`role_id`) references roles(`role_id`),
foreign key(`role_group_id`) references role_groups(`role_group_id`)
) engine=InnoDB;


Any help would be greatly appreciated.







mysql foreign-keys mysql-error-150






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 28 '17 at 19:03









Bill Karwin

369k60506663




369k60506663










asked Sep 21 '09 at 22:55







user176842















  • 1




    Could you post the error output and tell us which command (of the three) is causing the error?
    – dave
    Sep 21 '09 at 23:08






  • 4




    What's with the back-ticks around auto_increment? That's not valid. Auto_increment is a keyword, not an identifier.
    – Bill Karwin
    Sep 21 '09 at 23:39














  • 1




    Could you post the error output and tell us which command (of the three) is causing the error?
    – dave
    Sep 21 '09 at 23:08






  • 4




    What's with the back-ticks around auto_increment? That's not valid. Auto_increment is a keyword, not an identifier.
    – Bill Karwin
    Sep 21 '09 at 23:39








1




1




Could you post the error output and tell us which command (of the three) is causing the error?
– dave
Sep 21 '09 at 23:08




Could you post the error output and tell us which command (of the three) is causing the error?
– dave
Sep 21 '09 at 23:08




4




4




What's with the back-ticks around auto_increment? That's not valid. Auto_increment is a keyword, not an identifier.
– Bill Karwin
Sep 21 '09 at 23:39




What's with the back-ticks around auto_increment? That's not valid. Auto_increment is a keyword, not an identifier.
– Bill Karwin
Sep 21 '09 at 23:39












19 Answers
19






active

oldest

votes

















up vote
226
down vote













I had the same problem with ALTER TABLE ADD FOREIGN KEY.



After an hour, I found that these conditions must be satisfied to not get error 150:




  1. The Parent table must exist before you define a foreign key to reference it. You must define the tables in the right order: Parent table first, then the Child table. If both tables references each other, you must create one table without FK constraints, then create the second table, then add the FK constraint to the first table with ALTER TABLE.


  2. The two tables must both support foreign key constraints, i.e. ENGINE=InnoDB. Other storage engines silently ignore foreign key definitions, so they return no error or warning, but the FK constraint is not saved.


  3. The referenced columns in the Parent table must be the left-most columns of a key. Best if the key in the Parent is PRIMARY KEY or UNIQUE KEY.


  4. The FK definition must reference the PK column(s) in the same order as the PK definition. For example, if the FK REFERENCES Parent(a,b,c) then the Parent's PK must not be defined on columns in order (a,c,b).



  5. The PK column(s) in the Parent table must be the same data type as the FK column(s) in the Child table. For example, if a PK column in the Parent table is UNSIGNED, be sure to define UNSIGNED for the corresponding column in the Child table field.



    Exception: length of strings may be different. For example, VARCHAR(10) can reference VARCHAR(20) or vice versa.



  6. Any string-type FK column(s) must have the same character set and collation as the corresponding PK column(s).



  7. If there is data already in the Child table, every value in the FK column(s) must match a value in the Parent table PK column(s). Check this with a query like:



    SELECT COUNT(*) FROM Child LEFT OUTER JOIN Parent ON Child.FK = Parent.PK 
    WHERE Parent.PK IS NULL;


    This must return zero (0) unmatched values. Obviously, this query is an generic example; you must substitute your table names and column names.



  8. Neither the Parent table nor the Child table can be a TEMPORARY table.


  9. Neither the Parent table nor the Child table can be a PARTITIONED table.


  10. If you declare a FK with the ON DELETE SET NULL option, then the FK column(s) must be nullable.


  11. If you declare a constraint name for a foreign key, the constraint name must be unique in the whole schema, not only in the table in which the constraint is defined. Two tables may not have their own constraint with the same name.



Hope this helps.






share|improve this answer



















  • 3




    It does help, thank you.
    – Timo Huovinen
    May 9 '12 at 9:26








  • 4




    one more thing worth adding: if the PK of the parent table is more than one field, the order of the fields in the FK must be the same as the order in the PK
    – Kip
    May 9 '12 at 20:44








  • 25




    This includes things like int(11) unsigned NOT NULL vs int(11) NOT NULL.
    – Glen Solsberry
    May 1 '13 at 20:37






  • 4




    ALTER TABLE table_name ENGINE=InnoDB;
    – TolMera
    Jul 24 '13 at 3:15






  • 12




    If the table is defined ENGINE=MyISAM it doesn't generate errno 150 because it ignores foreign key declarations. It's like saying the best way to avoid trouble with your automobile engine is to drive a boat. :-)
    – Bill Karwin
    Oct 11 '13 at 17:18


















up vote
60
down vote













MySQL’s generic “errno 150” message “means that a foreign key constraint was not correctly formed.” As you probably already know if you are reading this page, the generic “errno: 150” error message is really unhelpful. However:



You can get the actual error message by running SHOW ENGINE INNODB STATUS; and then looking for LATEST FOREIGN KEY ERROR in the output.



For example, this attempt to create a foreign key constraint:



CREATE TABLE t1
(id INTEGER);

CREATE TABLE t2
(t1_id INTEGER,
CONSTRAINT FOREIGN KEY (t1_id) REFERENCES t1 (id));


fails with the error Can't create table 'test.t2' (errno: 150). That doesn’t tell anyone anything useful other than that it’s a foreign key problem. But run SHOW ENGINE INNODB STATUS; and it will say:



------------------------
LATEST FOREIGN KEY ERROR
------------------------
130811 23:36:38 Error in foreign key constraint of table test/t2:
FOREIGN KEY (t1_id) REFERENCES t1 (id)):
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.


It says that the problem is it can’t find an index. SHOW INDEX FROM t1 shows that there aren’t any indexes at all for table t1. Fix that by, say, defining a primary key on t1, and the foreign key constraint will be created successfully.






share|improve this answer

















  • 4




    SHOW ENGINE INNODB STATUS helped me immediately identify a problem I'd been trying to diagnose for nearly an hour. Thanks.
    – jatrim
    Aug 29 '14 at 0:09


















up vote
25
down vote













Make sure that the properties of the two fields you are trying to link with a constraint are exactly the same.



Often, the 'unsigned' property on an ID column will catch you out.



ALTER TABLE `dbname`.`tablename` CHANGE `fieldname` `fieldname` int(10) UNSIGNED NULL;





share|improve this answer



















  • 1




    thanks it was true.
    – Mahdi_Nine
    Mar 17 '11 at 11:46










  • In my experience, it's worth using MySQL's SHOW CREATE TABLE on your main table to check exactly what flags are set against your main index column, then copy them to your foreign key column. There might be things there, like "unsigned" that aren't obvious.
    – Ambulare
    Sep 29 '14 at 16:09


















up vote
10
down vote













What's the current state of your database when you run this script? Is it completely empty? Your SQL runs fine for me when creating a database from scratch, but errno 150 usually has to do with dropping & recreating tables that are part of a foreign key. I'm getting the feeling you're not working with a 100% fresh and new database.



If you're erroring out when "source"-ing your SQL file, you should be able to run the command "SHOW ENGINE INNODB STATUS" from the MySQL prompt immediately after the "source" command to see more detailed error info.



You may want to check out the manual entry too:




If you re-create a table that was dropped, it must have a definition that conforms to the foreign key constraints referencing it. It must have the right column names and types, and it must have indexes on the referenced keys, as stated earlier. If these are not satisfied, MySQL returns error number 1005 and refers to error 150 in the error message. If MySQL reports an error number 1005 from a CREATE TABLE statement, and the error message refers to error 150, table creation failed because a foreign key constraint was not correctly formed.



— MySQL 5.1 reference manual.







share|improve this answer






























    up vote
    5
    down vote













    For people who are viewing this thread with the same problem:



    There are a lot of reasons for getting errors like this. For a fairly complete list of causes and solutions of foreign key errors in MySQL (including those discussed here), check out this link:



    MySQL Foreign Key Errors and Errno 150






    share|improve this answer




























      up vote
      3
      down vote













      For others that find this SO entry via Google: Be sure that you aren't trying to do a SET NULL action on a foreign key (to be) column defined as "NOT NULL." That caused great frustration until I remembered to do a CHECK ENGINE INNODB STATUS.






      share|improve this answer




























        up vote
        3
        down vote













        Definitely it is not the case but I found this mistake pretty common and unobvious. The target of a FOREIGN KEY could be not PRIMARY KEY. Te answer which become useful for me is:



        A FOREIGN KEY always must be pointed to a PRIMARY KEY true field of other table.



        CREATE TABLE users(
        id INT AUTO_INCREMENT PRIMARY KEY,
        username VARCHAR(40));

        CREATE TABLE userroles(
        id INT AUTO_INCREMENT PRIMARY KEY,
        user_id INT NOT NULL,
        FOREIGN KEY(user_id) REFERENCES users(id));





        share|improve this answer





















        • This was my problem. Awesome error reporting, MySQL...
          – Brian Stinar
          Sep 12 '16 at 23:25


















        up vote
        3
        down vote













        As pointed by @andrewdotn the best way is to see the detailed error(SHOW ENGINE INNODB STATUS;) instead of just an error code.



        One of the reasons could be that an index already exists with the same name, may be in another table. As a practice, I recommend prefixing table name before the index name to avoid such collisions. e.g. instead of idx_userId use idx_userActionMapping_userId.






        share|improve this answer




























          up vote
          2
          down vote













          Helpful tip, use SHOW WARNINGS; after trying your CREATE query and you will receive the error as well as the more detailed warning:



              ---------------------------------------------------------------------------------------------------------+
          | Level | Code | Message |
          +---------+------+-------------------------------------------------------------------------- -------------------------------------------------------------------------------------------- ---------------+
          | Warning | 150 | Create table 'fakeDatabase/exampleTable' with foreign key constraint failed. There is no index in the referenced table where the referenced columns appear as the first columns.
          |
          | Error | 1005 | Can't create table 'exampleTable' (errno:150) |
          +---------+------+-------------------------------------------------------------------------- -------------------------------------------------------------------------------------------- ---------------+


          So in this case, time to re-create my table!






          share|improve this answer




























            up vote
            2
            down vote













            Please make sure at first that




            1. you are using InnoDB tables.

            2. field for FOREIGN KEY has the same type and length (!) as source field.


            I had the same trouble and I've fixed it. I had unsigned INT for one field and just integer for other field.






            share|improve this answer






























              up vote
              1
              down vote













              This is usually happening when you try to source file into existing database.
              Drop all the tables first (or the DB itself).
              And then source file with SET foreign_key_checks = 0; at the beginning and SET foreign_key_checks = 1; at the end.






              share|improve this answer




























                up vote
                1
                down vote













                I've found another reason this fails... case sensitive table names.



                For this table definition



                CREATE TABLE user (
                userId int PRIMARY KEY AUTO_INCREMENT,
                username varchar(30) NOT NULL
                ) ENGINE=InnoDB;


                This table definition works



                CREATE TABLE product (
                id int PRIMARY KEY AUTO_INCREMENT,
                userId int,
                FOREIGN KEY fkProductUser1(userId) REFERENCES **u**ser(userId)
                ) ENGINE=InnoDB;


                whereas this one fails



                CREATE TABLE product (
                id int PRIMARY KEY AUTO_INCREMENT,
                userId int,
                FOREIGN KEY fkProductUser1(userId) REFERENCES User(userId)
                ) ENGINE=InnoDB;


                The fact that it worked on Windows and failed on Unix took me a couple of hours to figure out. Hope that helps someone else.






                share|improve this answer




























                  up vote
                  1
                  down vote













                  MySQL Workbench 6.3 for Mac OS.



                  Problem: errno 150 on table X when trying to do Forward Engineering on a DB diagram, 20 out of 21 succeeded, 1 failed. If FKs on table X were deleted, the error moved to a different table that wasn't failing before.



                  Changed all tables engine to myISAM and it worked just fine.



                  enter image description here






                  share|improve this answer






























                    up vote
                    0
                    down vote













                    Also worth checking that you aren't accidentally operating on the wrong database. This error will occur if the foreign table does not exist. Why does MySQL have to be so cryptic?






                    share|improve this answer




























                      up vote
                      0
                      down vote













                      Make sure that the foreign keys are not listed as unique in the parent. I had this same problem and I solved it by demarcating it as not unique.






                      share|improve this answer




























                        up vote
                        0
                        down vote













                        In my case it was due to the fact that the field that was a foreign key field had a too long name, ie. foreign key (some_other_table_with_long_name_id). Try sth shorter. Error message is a bit misleading in that case.



                        Also, as @Jon mentioned earlier - field definitions have to be the same (watch out for unsigned subtype).






                        share|improve this answer




























                          up vote
                          0
                          down vote













                          When the foraign key constraint is based on varchar type, then in addition to the list provided by marv-el the target column must have an unique constraint.






                          share|improve this answer






























                            up vote
                            0
                            down vote













                            (Side notes too big for a Comment)



                            There is no need for an AUTO_INCREMENT id in a mapping table; get rid of it.



                            Change the PRIMARY KEY to (role_id, role_group_id) (in either order). This will make accesses faster.



                            Since you probably want to map both directions, also add an INDEX with those two columns in the opposite order. (There is no need to make it UNIQUE.)



                            More tips: http://mysql.rjweb.org/doc.php/index_cookbook_mysql#speeding_up_wp_postmeta






                            share|improve this answer




























                              up vote
                              -1
                              down vote













                              I encountered the same problem, but I check find that I hadn't the parent table. So I just edit the parent migration in front of the child migration. Just do it.






                              share|improve this answer





















                              • this should have been a comment instead of an answer
                                – hannad rehman
                                Jun 30 at 6:36










                              protected by Kermit Jul 10 '14 at 23:11



                              Thank you for your interest in this question.
                              Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                              Would you like to answer one of these unanswered questions instead?













                              19 Answers
                              19






                              active

                              oldest

                              votes








                              19 Answers
                              19






                              active

                              oldest

                              votes









                              active

                              oldest

                              votes






                              active

                              oldest

                              votes








                              up vote
                              226
                              down vote













                              I had the same problem with ALTER TABLE ADD FOREIGN KEY.



                              After an hour, I found that these conditions must be satisfied to not get error 150:




                              1. The Parent table must exist before you define a foreign key to reference it. You must define the tables in the right order: Parent table first, then the Child table. If both tables references each other, you must create one table without FK constraints, then create the second table, then add the FK constraint to the first table with ALTER TABLE.


                              2. The two tables must both support foreign key constraints, i.e. ENGINE=InnoDB. Other storage engines silently ignore foreign key definitions, so they return no error or warning, but the FK constraint is not saved.


                              3. The referenced columns in the Parent table must be the left-most columns of a key. Best if the key in the Parent is PRIMARY KEY or UNIQUE KEY.


                              4. The FK definition must reference the PK column(s) in the same order as the PK definition. For example, if the FK REFERENCES Parent(a,b,c) then the Parent's PK must not be defined on columns in order (a,c,b).



                              5. The PK column(s) in the Parent table must be the same data type as the FK column(s) in the Child table. For example, if a PK column in the Parent table is UNSIGNED, be sure to define UNSIGNED for the corresponding column in the Child table field.



                                Exception: length of strings may be different. For example, VARCHAR(10) can reference VARCHAR(20) or vice versa.



                              6. Any string-type FK column(s) must have the same character set and collation as the corresponding PK column(s).



                              7. If there is data already in the Child table, every value in the FK column(s) must match a value in the Parent table PK column(s). Check this with a query like:



                                SELECT COUNT(*) FROM Child LEFT OUTER JOIN Parent ON Child.FK = Parent.PK 
                                WHERE Parent.PK IS NULL;


                                This must return zero (0) unmatched values. Obviously, this query is an generic example; you must substitute your table names and column names.



                              8. Neither the Parent table nor the Child table can be a TEMPORARY table.


                              9. Neither the Parent table nor the Child table can be a PARTITIONED table.


                              10. If you declare a FK with the ON DELETE SET NULL option, then the FK column(s) must be nullable.


                              11. If you declare a constraint name for a foreign key, the constraint name must be unique in the whole schema, not only in the table in which the constraint is defined. Two tables may not have their own constraint with the same name.



                              Hope this helps.






                              share|improve this answer



















                              • 3




                                It does help, thank you.
                                – Timo Huovinen
                                May 9 '12 at 9:26








                              • 4




                                one more thing worth adding: if the PK of the parent table is more than one field, the order of the fields in the FK must be the same as the order in the PK
                                – Kip
                                May 9 '12 at 20:44








                              • 25




                                This includes things like int(11) unsigned NOT NULL vs int(11) NOT NULL.
                                – Glen Solsberry
                                May 1 '13 at 20:37






                              • 4




                                ALTER TABLE table_name ENGINE=InnoDB;
                                – TolMera
                                Jul 24 '13 at 3:15






                              • 12




                                If the table is defined ENGINE=MyISAM it doesn't generate errno 150 because it ignores foreign key declarations. It's like saying the best way to avoid trouble with your automobile engine is to drive a boat. :-)
                                – Bill Karwin
                                Oct 11 '13 at 17:18















                              up vote
                              226
                              down vote













                              I had the same problem with ALTER TABLE ADD FOREIGN KEY.



                              After an hour, I found that these conditions must be satisfied to not get error 150:




                              1. The Parent table must exist before you define a foreign key to reference it. You must define the tables in the right order: Parent table first, then the Child table. If both tables references each other, you must create one table without FK constraints, then create the second table, then add the FK constraint to the first table with ALTER TABLE.


                              2. The two tables must both support foreign key constraints, i.e. ENGINE=InnoDB. Other storage engines silently ignore foreign key definitions, so they return no error or warning, but the FK constraint is not saved.


                              3. The referenced columns in the Parent table must be the left-most columns of a key. Best if the key in the Parent is PRIMARY KEY or UNIQUE KEY.


                              4. The FK definition must reference the PK column(s) in the same order as the PK definition. For example, if the FK REFERENCES Parent(a,b,c) then the Parent's PK must not be defined on columns in order (a,c,b).



                              5. The PK column(s) in the Parent table must be the same data type as the FK column(s) in the Child table. For example, if a PK column in the Parent table is UNSIGNED, be sure to define UNSIGNED for the corresponding column in the Child table field.



                                Exception: length of strings may be different. For example, VARCHAR(10) can reference VARCHAR(20) or vice versa.



                              6. Any string-type FK column(s) must have the same character set and collation as the corresponding PK column(s).



                              7. If there is data already in the Child table, every value in the FK column(s) must match a value in the Parent table PK column(s). Check this with a query like:



                                SELECT COUNT(*) FROM Child LEFT OUTER JOIN Parent ON Child.FK = Parent.PK 
                                WHERE Parent.PK IS NULL;


                                This must return zero (0) unmatched values. Obviously, this query is an generic example; you must substitute your table names and column names.



                              8. Neither the Parent table nor the Child table can be a TEMPORARY table.


                              9. Neither the Parent table nor the Child table can be a PARTITIONED table.


                              10. If you declare a FK with the ON DELETE SET NULL option, then the FK column(s) must be nullable.


                              11. If you declare a constraint name for a foreign key, the constraint name must be unique in the whole schema, not only in the table in which the constraint is defined. Two tables may not have their own constraint with the same name.



                              Hope this helps.






                              share|improve this answer



















                              • 3




                                It does help, thank you.
                                – Timo Huovinen
                                May 9 '12 at 9:26








                              • 4




                                one more thing worth adding: if the PK of the parent table is more than one field, the order of the fields in the FK must be the same as the order in the PK
                                – Kip
                                May 9 '12 at 20:44








                              • 25




                                This includes things like int(11) unsigned NOT NULL vs int(11) NOT NULL.
                                – Glen Solsberry
                                May 1 '13 at 20:37






                              • 4




                                ALTER TABLE table_name ENGINE=InnoDB;
                                – TolMera
                                Jul 24 '13 at 3:15






                              • 12




                                If the table is defined ENGINE=MyISAM it doesn't generate errno 150 because it ignores foreign key declarations. It's like saying the best way to avoid trouble with your automobile engine is to drive a boat. :-)
                                – Bill Karwin
                                Oct 11 '13 at 17:18













                              up vote
                              226
                              down vote










                              up vote
                              226
                              down vote









                              I had the same problem with ALTER TABLE ADD FOREIGN KEY.



                              After an hour, I found that these conditions must be satisfied to not get error 150:




                              1. The Parent table must exist before you define a foreign key to reference it. You must define the tables in the right order: Parent table first, then the Child table. If both tables references each other, you must create one table without FK constraints, then create the second table, then add the FK constraint to the first table with ALTER TABLE.


                              2. The two tables must both support foreign key constraints, i.e. ENGINE=InnoDB. Other storage engines silently ignore foreign key definitions, so they return no error or warning, but the FK constraint is not saved.


                              3. The referenced columns in the Parent table must be the left-most columns of a key. Best if the key in the Parent is PRIMARY KEY or UNIQUE KEY.


                              4. The FK definition must reference the PK column(s) in the same order as the PK definition. For example, if the FK REFERENCES Parent(a,b,c) then the Parent's PK must not be defined on columns in order (a,c,b).



                              5. The PK column(s) in the Parent table must be the same data type as the FK column(s) in the Child table. For example, if a PK column in the Parent table is UNSIGNED, be sure to define UNSIGNED for the corresponding column in the Child table field.



                                Exception: length of strings may be different. For example, VARCHAR(10) can reference VARCHAR(20) or vice versa.



                              6. Any string-type FK column(s) must have the same character set and collation as the corresponding PK column(s).



                              7. If there is data already in the Child table, every value in the FK column(s) must match a value in the Parent table PK column(s). Check this with a query like:



                                SELECT COUNT(*) FROM Child LEFT OUTER JOIN Parent ON Child.FK = Parent.PK 
                                WHERE Parent.PK IS NULL;


                                This must return zero (0) unmatched values. Obviously, this query is an generic example; you must substitute your table names and column names.



                              8. Neither the Parent table nor the Child table can be a TEMPORARY table.


                              9. Neither the Parent table nor the Child table can be a PARTITIONED table.


                              10. If you declare a FK with the ON DELETE SET NULL option, then the FK column(s) must be nullable.


                              11. If you declare a constraint name for a foreign key, the constraint name must be unique in the whole schema, not only in the table in which the constraint is defined. Two tables may not have their own constraint with the same name.



                              Hope this helps.






                              share|improve this answer














                              I had the same problem with ALTER TABLE ADD FOREIGN KEY.



                              After an hour, I found that these conditions must be satisfied to not get error 150:




                              1. The Parent table must exist before you define a foreign key to reference it. You must define the tables in the right order: Parent table first, then the Child table. If both tables references each other, you must create one table without FK constraints, then create the second table, then add the FK constraint to the first table with ALTER TABLE.


                              2. The two tables must both support foreign key constraints, i.e. ENGINE=InnoDB. Other storage engines silently ignore foreign key definitions, so they return no error or warning, but the FK constraint is not saved.


                              3. The referenced columns in the Parent table must be the left-most columns of a key. Best if the key in the Parent is PRIMARY KEY or UNIQUE KEY.


                              4. The FK definition must reference the PK column(s) in the same order as the PK definition. For example, if the FK REFERENCES Parent(a,b,c) then the Parent's PK must not be defined on columns in order (a,c,b).



                              5. The PK column(s) in the Parent table must be the same data type as the FK column(s) in the Child table. For example, if a PK column in the Parent table is UNSIGNED, be sure to define UNSIGNED for the corresponding column in the Child table field.



                                Exception: length of strings may be different. For example, VARCHAR(10) can reference VARCHAR(20) or vice versa.



                              6. Any string-type FK column(s) must have the same character set and collation as the corresponding PK column(s).



                              7. If there is data already in the Child table, every value in the FK column(s) must match a value in the Parent table PK column(s). Check this with a query like:



                                SELECT COUNT(*) FROM Child LEFT OUTER JOIN Parent ON Child.FK = Parent.PK 
                                WHERE Parent.PK IS NULL;


                                This must return zero (0) unmatched values. Obviously, this query is an generic example; you must substitute your table names and column names.



                              8. Neither the Parent table nor the Child table can be a TEMPORARY table.


                              9. Neither the Parent table nor the Child table can be a PARTITIONED table.


                              10. If you declare a FK with the ON DELETE SET NULL option, then the FK column(s) must be nullable.


                              11. If you declare a constraint name for a foreign key, the constraint name must be unique in the whole schema, not only in the table in which the constraint is defined. Two tables may not have their own constraint with the same name.



                              Hope this helps.







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Mar 18 at 20:52









                              Bill Karwin

                              369k60506663




                              369k60506663










                              answered Jan 12 '11 at 20:39









                              marv-el

                              2,261192




                              2,261192








                              • 3




                                It does help, thank you.
                                – Timo Huovinen
                                May 9 '12 at 9:26








                              • 4




                                one more thing worth adding: if the PK of the parent table is more than one field, the order of the fields in the FK must be the same as the order in the PK
                                – Kip
                                May 9 '12 at 20:44








                              • 25




                                This includes things like int(11) unsigned NOT NULL vs int(11) NOT NULL.
                                – Glen Solsberry
                                May 1 '13 at 20:37






                              • 4




                                ALTER TABLE table_name ENGINE=InnoDB;
                                – TolMera
                                Jul 24 '13 at 3:15






                              • 12




                                If the table is defined ENGINE=MyISAM it doesn't generate errno 150 because it ignores foreign key declarations. It's like saying the best way to avoid trouble with your automobile engine is to drive a boat. :-)
                                – Bill Karwin
                                Oct 11 '13 at 17:18














                              • 3




                                It does help, thank you.
                                – Timo Huovinen
                                May 9 '12 at 9:26








                              • 4




                                one more thing worth adding: if the PK of the parent table is more than one field, the order of the fields in the FK must be the same as the order in the PK
                                – Kip
                                May 9 '12 at 20:44








                              • 25




                                This includes things like int(11) unsigned NOT NULL vs int(11) NOT NULL.
                                – Glen Solsberry
                                May 1 '13 at 20:37






                              • 4




                                ALTER TABLE table_name ENGINE=InnoDB;
                                – TolMera
                                Jul 24 '13 at 3:15






                              • 12




                                If the table is defined ENGINE=MyISAM it doesn't generate errno 150 because it ignores foreign key declarations. It's like saying the best way to avoid trouble with your automobile engine is to drive a boat. :-)
                                – Bill Karwin
                                Oct 11 '13 at 17:18








                              3




                              3




                              It does help, thank you.
                              – Timo Huovinen
                              May 9 '12 at 9:26






                              It does help, thank you.
                              – Timo Huovinen
                              May 9 '12 at 9:26






                              4




                              4




                              one more thing worth adding: if the PK of the parent table is more than one field, the order of the fields in the FK must be the same as the order in the PK
                              – Kip
                              May 9 '12 at 20:44






                              one more thing worth adding: if the PK of the parent table is more than one field, the order of the fields in the FK must be the same as the order in the PK
                              – Kip
                              May 9 '12 at 20:44






                              25




                              25




                              This includes things like int(11) unsigned NOT NULL vs int(11) NOT NULL.
                              – Glen Solsberry
                              May 1 '13 at 20:37




                              This includes things like int(11) unsigned NOT NULL vs int(11) NOT NULL.
                              – Glen Solsberry
                              May 1 '13 at 20:37




                              4




                              4




                              ALTER TABLE table_name ENGINE=InnoDB;
                              – TolMera
                              Jul 24 '13 at 3:15




                              ALTER TABLE table_name ENGINE=InnoDB;
                              – TolMera
                              Jul 24 '13 at 3:15




                              12




                              12




                              If the table is defined ENGINE=MyISAM it doesn't generate errno 150 because it ignores foreign key declarations. It's like saying the best way to avoid trouble with your automobile engine is to drive a boat. :-)
                              – Bill Karwin
                              Oct 11 '13 at 17:18




                              If the table is defined ENGINE=MyISAM it doesn't generate errno 150 because it ignores foreign key declarations. It's like saying the best way to avoid trouble with your automobile engine is to drive a boat. :-)
                              – Bill Karwin
                              Oct 11 '13 at 17:18












                              up vote
                              60
                              down vote













                              MySQL’s generic “errno 150” message “means that a foreign key constraint was not correctly formed.” As you probably already know if you are reading this page, the generic “errno: 150” error message is really unhelpful. However:



                              You can get the actual error message by running SHOW ENGINE INNODB STATUS; and then looking for LATEST FOREIGN KEY ERROR in the output.



                              For example, this attempt to create a foreign key constraint:



                              CREATE TABLE t1
                              (id INTEGER);

                              CREATE TABLE t2
                              (t1_id INTEGER,
                              CONSTRAINT FOREIGN KEY (t1_id) REFERENCES t1 (id));


                              fails with the error Can't create table 'test.t2' (errno: 150). That doesn’t tell anyone anything useful other than that it’s a foreign key problem. But run SHOW ENGINE INNODB STATUS; and it will say:



                              ------------------------
                              LATEST FOREIGN KEY ERROR
                              ------------------------
                              130811 23:36:38 Error in foreign key constraint of table test/t2:
                              FOREIGN KEY (t1_id) REFERENCES t1 (id)):
                              Cannot find an index in the referenced table where the
                              referenced columns appear as the first columns, or column types
                              in the table and the referenced table do not match for constraint.


                              It says that the problem is it can’t find an index. SHOW INDEX FROM t1 shows that there aren’t any indexes at all for table t1. Fix that by, say, defining a primary key on t1, and the foreign key constraint will be created successfully.






                              share|improve this answer

















                              • 4




                                SHOW ENGINE INNODB STATUS helped me immediately identify a problem I'd been trying to diagnose for nearly an hour. Thanks.
                                – jatrim
                                Aug 29 '14 at 0:09















                              up vote
                              60
                              down vote













                              MySQL’s generic “errno 150” message “means that a foreign key constraint was not correctly formed.” As you probably already know if you are reading this page, the generic “errno: 150” error message is really unhelpful. However:



                              You can get the actual error message by running SHOW ENGINE INNODB STATUS; and then looking for LATEST FOREIGN KEY ERROR in the output.



                              For example, this attempt to create a foreign key constraint:



                              CREATE TABLE t1
                              (id INTEGER);

                              CREATE TABLE t2
                              (t1_id INTEGER,
                              CONSTRAINT FOREIGN KEY (t1_id) REFERENCES t1 (id));


                              fails with the error Can't create table 'test.t2' (errno: 150). That doesn’t tell anyone anything useful other than that it’s a foreign key problem. But run SHOW ENGINE INNODB STATUS; and it will say:



                              ------------------------
                              LATEST FOREIGN KEY ERROR
                              ------------------------
                              130811 23:36:38 Error in foreign key constraint of table test/t2:
                              FOREIGN KEY (t1_id) REFERENCES t1 (id)):
                              Cannot find an index in the referenced table where the
                              referenced columns appear as the first columns, or column types
                              in the table and the referenced table do not match for constraint.


                              It says that the problem is it can’t find an index. SHOW INDEX FROM t1 shows that there aren’t any indexes at all for table t1. Fix that by, say, defining a primary key on t1, and the foreign key constraint will be created successfully.






                              share|improve this answer

















                              • 4




                                SHOW ENGINE INNODB STATUS helped me immediately identify a problem I'd been trying to diagnose for nearly an hour. Thanks.
                                – jatrim
                                Aug 29 '14 at 0:09













                              up vote
                              60
                              down vote










                              up vote
                              60
                              down vote









                              MySQL’s generic “errno 150” message “means that a foreign key constraint was not correctly formed.” As you probably already know if you are reading this page, the generic “errno: 150” error message is really unhelpful. However:



                              You can get the actual error message by running SHOW ENGINE INNODB STATUS; and then looking for LATEST FOREIGN KEY ERROR in the output.



                              For example, this attempt to create a foreign key constraint:



                              CREATE TABLE t1
                              (id INTEGER);

                              CREATE TABLE t2
                              (t1_id INTEGER,
                              CONSTRAINT FOREIGN KEY (t1_id) REFERENCES t1 (id));


                              fails with the error Can't create table 'test.t2' (errno: 150). That doesn’t tell anyone anything useful other than that it’s a foreign key problem. But run SHOW ENGINE INNODB STATUS; and it will say:



                              ------------------------
                              LATEST FOREIGN KEY ERROR
                              ------------------------
                              130811 23:36:38 Error in foreign key constraint of table test/t2:
                              FOREIGN KEY (t1_id) REFERENCES t1 (id)):
                              Cannot find an index in the referenced table where the
                              referenced columns appear as the first columns, or column types
                              in the table and the referenced table do not match for constraint.


                              It says that the problem is it can’t find an index. SHOW INDEX FROM t1 shows that there aren’t any indexes at all for table t1. Fix that by, say, defining a primary key on t1, and the foreign key constraint will be created successfully.






                              share|improve this answer












                              MySQL’s generic “errno 150” message “means that a foreign key constraint was not correctly formed.” As you probably already know if you are reading this page, the generic “errno: 150” error message is really unhelpful. However:



                              You can get the actual error message by running SHOW ENGINE INNODB STATUS; and then looking for LATEST FOREIGN KEY ERROR in the output.



                              For example, this attempt to create a foreign key constraint:



                              CREATE TABLE t1
                              (id INTEGER);

                              CREATE TABLE t2
                              (t1_id INTEGER,
                              CONSTRAINT FOREIGN KEY (t1_id) REFERENCES t1 (id));


                              fails with the error Can't create table 'test.t2' (errno: 150). That doesn’t tell anyone anything useful other than that it’s a foreign key problem. But run SHOW ENGINE INNODB STATUS; and it will say:



                              ------------------------
                              LATEST FOREIGN KEY ERROR
                              ------------------------
                              130811 23:36:38 Error in foreign key constraint of table test/t2:
                              FOREIGN KEY (t1_id) REFERENCES t1 (id)):
                              Cannot find an index in the referenced table where the
                              referenced columns appear as the first columns, or column types
                              in the table and the referenced table do not match for constraint.


                              It says that the problem is it can’t find an index. SHOW INDEX FROM t1 shows that there aren’t any indexes at all for table t1. Fix that by, say, defining a primary key on t1, and the foreign key constraint will be created successfully.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Aug 12 '13 at 5:47









                              andrewdotn

                              22.5k16597




                              22.5k16597








                              • 4




                                SHOW ENGINE INNODB STATUS helped me immediately identify a problem I'd been trying to diagnose for nearly an hour. Thanks.
                                – jatrim
                                Aug 29 '14 at 0:09














                              • 4




                                SHOW ENGINE INNODB STATUS helped me immediately identify a problem I'd been trying to diagnose for nearly an hour. Thanks.
                                – jatrim
                                Aug 29 '14 at 0:09








                              4




                              4




                              SHOW ENGINE INNODB STATUS helped me immediately identify a problem I'd been trying to diagnose for nearly an hour. Thanks.
                              – jatrim
                              Aug 29 '14 at 0:09




                              SHOW ENGINE INNODB STATUS helped me immediately identify a problem I'd been trying to diagnose for nearly an hour. Thanks.
                              – jatrim
                              Aug 29 '14 at 0:09










                              up vote
                              25
                              down vote













                              Make sure that the properties of the two fields you are trying to link with a constraint are exactly the same.



                              Often, the 'unsigned' property on an ID column will catch you out.



                              ALTER TABLE `dbname`.`tablename` CHANGE `fieldname` `fieldname` int(10) UNSIGNED NULL;





                              share|improve this answer



















                              • 1




                                thanks it was true.
                                – Mahdi_Nine
                                Mar 17 '11 at 11:46










                              • In my experience, it's worth using MySQL's SHOW CREATE TABLE on your main table to check exactly what flags are set against your main index column, then copy them to your foreign key column. There might be things there, like "unsigned" that aren't obvious.
                                – Ambulare
                                Sep 29 '14 at 16:09















                              up vote
                              25
                              down vote













                              Make sure that the properties of the two fields you are trying to link with a constraint are exactly the same.



                              Often, the 'unsigned' property on an ID column will catch you out.



                              ALTER TABLE `dbname`.`tablename` CHANGE `fieldname` `fieldname` int(10) UNSIGNED NULL;





                              share|improve this answer



















                              • 1




                                thanks it was true.
                                – Mahdi_Nine
                                Mar 17 '11 at 11:46










                              • In my experience, it's worth using MySQL's SHOW CREATE TABLE on your main table to check exactly what flags are set against your main index column, then copy them to your foreign key column. There might be things there, like "unsigned" that aren't obvious.
                                – Ambulare
                                Sep 29 '14 at 16:09













                              up vote
                              25
                              down vote










                              up vote
                              25
                              down vote









                              Make sure that the properties of the two fields you are trying to link with a constraint are exactly the same.



                              Often, the 'unsigned' property on an ID column will catch you out.



                              ALTER TABLE `dbname`.`tablename` CHANGE `fieldname` `fieldname` int(10) UNSIGNED NULL;





                              share|improve this answer














                              Make sure that the properties of the two fields you are trying to link with a constraint are exactly the same.



                              Often, the 'unsigned' property on an ID column will catch you out.



                              ALTER TABLE `dbname`.`tablename` CHANGE `fieldname` `fieldname` int(10) UNSIGNED NULL;






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Jan 11 '13 at 10:31

























                              answered Oct 27 '09 at 15:58









                              Jon Winstanley

                              16.1k1764105




                              16.1k1764105








                              • 1




                                thanks it was true.
                                – Mahdi_Nine
                                Mar 17 '11 at 11:46










                              • In my experience, it's worth using MySQL's SHOW CREATE TABLE on your main table to check exactly what flags are set against your main index column, then copy them to your foreign key column. There might be things there, like "unsigned" that aren't obvious.
                                – Ambulare
                                Sep 29 '14 at 16:09














                              • 1




                                thanks it was true.
                                – Mahdi_Nine
                                Mar 17 '11 at 11:46










                              • In my experience, it's worth using MySQL's SHOW CREATE TABLE on your main table to check exactly what flags are set against your main index column, then copy them to your foreign key column. There might be things there, like "unsigned" that aren't obvious.
                                – Ambulare
                                Sep 29 '14 at 16:09








                              1




                              1




                              thanks it was true.
                              – Mahdi_Nine
                              Mar 17 '11 at 11:46




                              thanks it was true.
                              – Mahdi_Nine
                              Mar 17 '11 at 11:46












                              In my experience, it's worth using MySQL's SHOW CREATE TABLE on your main table to check exactly what flags are set against your main index column, then copy them to your foreign key column. There might be things there, like "unsigned" that aren't obvious.
                              – Ambulare
                              Sep 29 '14 at 16:09




                              In my experience, it's worth using MySQL's SHOW CREATE TABLE on your main table to check exactly what flags are set against your main index column, then copy them to your foreign key column. There might be things there, like "unsigned" that aren't obvious.
                              – Ambulare
                              Sep 29 '14 at 16:09










                              up vote
                              10
                              down vote













                              What's the current state of your database when you run this script? Is it completely empty? Your SQL runs fine for me when creating a database from scratch, but errno 150 usually has to do with dropping & recreating tables that are part of a foreign key. I'm getting the feeling you're not working with a 100% fresh and new database.



                              If you're erroring out when "source"-ing your SQL file, you should be able to run the command "SHOW ENGINE INNODB STATUS" from the MySQL prompt immediately after the "source" command to see more detailed error info.



                              You may want to check out the manual entry too:




                              If you re-create a table that was dropped, it must have a definition that conforms to the foreign key constraints referencing it. It must have the right column names and types, and it must have indexes on the referenced keys, as stated earlier. If these are not satisfied, MySQL returns error number 1005 and refers to error 150 in the error message. If MySQL reports an error number 1005 from a CREATE TABLE statement, and the error message refers to error 150, table creation failed because a foreign key constraint was not correctly formed.



                              — MySQL 5.1 reference manual.







                              share|improve this answer



























                                up vote
                                10
                                down vote













                                What's the current state of your database when you run this script? Is it completely empty? Your SQL runs fine for me when creating a database from scratch, but errno 150 usually has to do with dropping & recreating tables that are part of a foreign key. I'm getting the feeling you're not working with a 100% fresh and new database.



                                If you're erroring out when "source"-ing your SQL file, you should be able to run the command "SHOW ENGINE INNODB STATUS" from the MySQL prompt immediately after the "source" command to see more detailed error info.



                                You may want to check out the manual entry too:




                                If you re-create a table that was dropped, it must have a definition that conforms to the foreign key constraints referencing it. It must have the right column names and types, and it must have indexes on the referenced keys, as stated earlier. If these are not satisfied, MySQL returns error number 1005 and refers to error 150 in the error message. If MySQL reports an error number 1005 from a CREATE TABLE statement, and the error message refers to error 150, table creation failed because a foreign key constraint was not correctly formed.



                                — MySQL 5.1 reference manual.







                                share|improve this answer

























                                  up vote
                                  10
                                  down vote










                                  up vote
                                  10
                                  down vote









                                  What's the current state of your database when you run this script? Is it completely empty? Your SQL runs fine for me when creating a database from scratch, but errno 150 usually has to do with dropping & recreating tables that are part of a foreign key. I'm getting the feeling you're not working with a 100% fresh and new database.



                                  If you're erroring out when "source"-ing your SQL file, you should be able to run the command "SHOW ENGINE INNODB STATUS" from the MySQL prompt immediately after the "source" command to see more detailed error info.



                                  You may want to check out the manual entry too:




                                  If you re-create a table that was dropped, it must have a definition that conforms to the foreign key constraints referencing it. It must have the right column names and types, and it must have indexes on the referenced keys, as stated earlier. If these are not satisfied, MySQL returns error number 1005 and refers to error 150 in the error message. If MySQL reports an error number 1005 from a CREATE TABLE statement, and the error message refers to error 150, table creation failed because a foreign key constraint was not correctly formed.



                                  — MySQL 5.1 reference manual.







                                  share|improve this answer














                                  What's the current state of your database when you run this script? Is it completely empty? Your SQL runs fine for me when creating a database from scratch, but errno 150 usually has to do with dropping & recreating tables that are part of a foreign key. I'm getting the feeling you're not working with a 100% fresh and new database.



                                  If you're erroring out when "source"-ing your SQL file, you should be able to run the command "SHOW ENGINE INNODB STATUS" from the MySQL prompt immediately after the "source" command to see more detailed error info.



                                  You may want to check out the manual entry too:




                                  If you re-create a table that was dropped, it must have a definition that conforms to the foreign key constraints referencing it. It must have the right column names and types, and it must have indexes on the referenced keys, as stated earlier. If these are not satisfied, MySQL returns error number 1005 and refers to error 150 in the error message. If MySQL reports an error number 1005 from a CREATE TABLE statement, and the error message refers to error 150, table creation failed because a foreign key constraint was not correctly formed.



                                  — MySQL 5.1 reference manual.








                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited Jan 19 '16 at 16:32









                                  Sergey Brunov

                                  9,70343065




                                  9,70343065










                                  answered Sep 21 '09 at 23:14









                                  Brent Writes Code

                                  12.3k54152




                                  12.3k54152






















                                      up vote
                                      5
                                      down vote













                                      For people who are viewing this thread with the same problem:



                                      There are a lot of reasons for getting errors like this. For a fairly complete list of causes and solutions of foreign key errors in MySQL (including those discussed here), check out this link:



                                      MySQL Foreign Key Errors and Errno 150






                                      share|improve this answer

























                                        up vote
                                        5
                                        down vote













                                        For people who are viewing this thread with the same problem:



                                        There are a lot of reasons for getting errors like this. For a fairly complete list of causes and solutions of foreign key errors in MySQL (including those discussed here), check out this link:



                                        MySQL Foreign Key Errors and Errno 150






                                        share|improve this answer























                                          up vote
                                          5
                                          down vote










                                          up vote
                                          5
                                          down vote









                                          For people who are viewing this thread with the same problem:



                                          There are a lot of reasons for getting errors like this. For a fairly complete list of causes and solutions of foreign key errors in MySQL (including those discussed here), check out this link:



                                          MySQL Foreign Key Errors and Errno 150






                                          share|improve this answer












                                          For people who are viewing this thread with the same problem:



                                          There are a lot of reasons for getting errors like this. For a fairly complete list of causes and solutions of foreign key errors in MySQL (including those discussed here), check out this link:



                                          MySQL Foreign Key Errors and Errno 150







                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered Jun 12 '12 at 23:44









                                          juacala

                                          1,4171315




                                          1,4171315






















                                              up vote
                                              3
                                              down vote













                                              For others that find this SO entry via Google: Be sure that you aren't trying to do a SET NULL action on a foreign key (to be) column defined as "NOT NULL." That caused great frustration until I remembered to do a CHECK ENGINE INNODB STATUS.






                                              share|improve this answer

























                                                up vote
                                                3
                                                down vote













                                                For others that find this SO entry via Google: Be sure that you aren't trying to do a SET NULL action on a foreign key (to be) column defined as "NOT NULL." That caused great frustration until I remembered to do a CHECK ENGINE INNODB STATUS.






                                                share|improve this answer























                                                  up vote
                                                  3
                                                  down vote










                                                  up vote
                                                  3
                                                  down vote









                                                  For others that find this SO entry via Google: Be sure that you aren't trying to do a SET NULL action on a foreign key (to be) column defined as "NOT NULL." That caused great frustration until I remembered to do a CHECK ENGINE INNODB STATUS.






                                                  share|improve this answer












                                                  For others that find this SO entry via Google: Be sure that you aren't trying to do a SET NULL action on a foreign key (to be) column defined as "NOT NULL." That caused great frustration until I remembered to do a CHECK ENGINE INNODB STATUS.







                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Feb 23 '12 at 12:48









                                                  Eric Lawler

                                                  1,9341619




                                                  1,9341619






















                                                      up vote
                                                      3
                                                      down vote













                                                      Definitely it is not the case but I found this mistake pretty common and unobvious. The target of a FOREIGN KEY could be not PRIMARY KEY. Te answer which become useful for me is:



                                                      A FOREIGN KEY always must be pointed to a PRIMARY KEY true field of other table.



                                                      CREATE TABLE users(
                                                      id INT AUTO_INCREMENT PRIMARY KEY,
                                                      username VARCHAR(40));

                                                      CREATE TABLE userroles(
                                                      id INT AUTO_INCREMENT PRIMARY KEY,
                                                      user_id INT NOT NULL,
                                                      FOREIGN KEY(user_id) REFERENCES users(id));





                                                      share|improve this answer





















                                                      • This was my problem. Awesome error reporting, MySQL...
                                                        – Brian Stinar
                                                        Sep 12 '16 at 23:25















                                                      up vote
                                                      3
                                                      down vote













                                                      Definitely it is not the case but I found this mistake pretty common and unobvious. The target of a FOREIGN KEY could be not PRIMARY KEY. Te answer which become useful for me is:



                                                      A FOREIGN KEY always must be pointed to a PRIMARY KEY true field of other table.



                                                      CREATE TABLE users(
                                                      id INT AUTO_INCREMENT PRIMARY KEY,
                                                      username VARCHAR(40));

                                                      CREATE TABLE userroles(
                                                      id INT AUTO_INCREMENT PRIMARY KEY,
                                                      user_id INT NOT NULL,
                                                      FOREIGN KEY(user_id) REFERENCES users(id));





                                                      share|improve this answer





















                                                      • This was my problem. Awesome error reporting, MySQL...
                                                        – Brian Stinar
                                                        Sep 12 '16 at 23:25













                                                      up vote
                                                      3
                                                      down vote










                                                      up vote
                                                      3
                                                      down vote









                                                      Definitely it is not the case but I found this mistake pretty common and unobvious. The target of a FOREIGN KEY could be not PRIMARY KEY. Te answer which become useful for me is:



                                                      A FOREIGN KEY always must be pointed to a PRIMARY KEY true field of other table.



                                                      CREATE TABLE users(
                                                      id INT AUTO_INCREMENT PRIMARY KEY,
                                                      username VARCHAR(40));

                                                      CREATE TABLE userroles(
                                                      id INT AUTO_INCREMENT PRIMARY KEY,
                                                      user_id INT NOT NULL,
                                                      FOREIGN KEY(user_id) REFERENCES users(id));





                                                      share|improve this answer












                                                      Definitely it is not the case but I found this mistake pretty common and unobvious. The target of a FOREIGN KEY could be not PRIMARY KEY. Te answer which become useful for me is:



                                                      A FOREIGN KEY always must be pointed to a PRIMARY KEY true field of other table.



                                                      CREATE TABLE users(
                                                      id INT AUTO_INCREMENT PRIMARY KEY,
                                                      username VARCHAR(40));

                                                      CREATE TABLE userroles(
                                                      id INT AUTO_INCREMENT PRIMARY KEY,
                                                      user_id INT NOT NULL,
                                                      FOREIGN KEY(user_id) REFERENCES users(id));






                                                      share|improve this answer












                                                      share|improve this answer



                                                      share|improve this answer










                                                      answered Nov 26 '14 at 14:22









                                                      I159

                                                      9,354227399




                                                      9,354227399












                                                      • This was my problem. Awesome error reporting, MySQL...
                                                        – Brian Stinar
                                                        Sep 12 '16 at 23:25


















                                                      • This was my problem. Awesome error reporting, MySQL...
                                                        – Brian Stinar
                                                        Sep 12 '16 at 23:25
















                                                      This was my problem. Awesome error reporting, MySQL...
                                                      – Brian Stinar
                                                      Sep 12 '16 at 23:25




                                                      This was my problem. Awesome error reporting, MySQL...
                                                      – Brian Stinar
                                                      Sep 12 '16 at 23:25










                                                      up vote
                                                      3
                                                      down vote













                                                      As pointed by @andrewdotn the best way is to see the detailed error(SHOW ENGINE INNODB STATUS;) instead of just an error code.



                                                      One of the reasons could be that an index already exists with the same name, may be in another table. As a practice, I recommend prefixing table name before the index name to avoid such collisions. e.g. instead of idx_userId use idx_userActionMapping_userId.






                                                      share|improve this answer

























                                                        up vote
                                                        3
                                                        down vote













                                                        As pointed by @andrewdotn the best way is to see the detailed error(SHOW ENGINE INNODB STATUS;) instead of just an error code.



                                                        One of the reasons could be that an index already exists with the same name, may be in another table. As a practice, I recommend prefixing table name before the index name to avoid such collisions. e.g. instead of idx_userId use idx_userActionMapping_userId.






                                                        share|improve this answer























                                                          up vote
                                                          3
                                                          down vote










                                                          up vote
                                                          3
                                                          down vote









                                                          As pointed by @andrewdotn the best way is to see the detailed error(SHOW ENGINE INNODB STATUS;) instead of just an error code.



                                                          One of the reasons could be that an index already exists with the same name, may be in another table. As a practice, I recommend prefixing table name before the index name to avoid such collisions. e.g. instead of idx_userId use idx_userActionMapping_userId.






                                                          share|improve this answer












                                                          As pointed by @andrewdotn the best way is to see the detailed error(SHOW ENGINE INNODB STATUS;) instead of just an error code.



                                                          One of the reasons could be that an index already exists with the same name, may be in another table. As a practice, I recommend prefixing table name before the index name to avoid such collisions. e.g. instead of idx_userId use idx_userActionMapping_userId.







                                                          share|improve this answer












                                                          share|improve this answer



                                                          share|improve this answer










                                                          answered Jun 4 '15 at 4:18









                                                          MuchMore

                                                          13814




                                                          13814






















                                                              up vote
                                                              2
                                                              down vote













                                                              Helpful tip, use SHOW WARNINGS; after trying your CREATE query and you will receive the error as well as the more detailed warning:



                                                                  ---------------------------------------------------------------------------------------------------------+
                                                              | Level | Code | Message |
                                                              +---------+------+-------------------------------------------------------------------------- -------------------------------------------------------------------------------------------- ---------------+
                                                              | Warning | 150 | Create table 'fakeDatabase/exampleTable' with foreign key constraint failed. There is no index in the referenced table where the referenced columns appear as the first columns.
                                                              |
                                                              | Error | 1005 | Can't create table 'exampleTable' (errno:150) |
                                                              +---------+------+-------------------------------------------------------------------------- -------------------------------------------------------------------------------------------- ---------------+


                                                              So in this case, time to re-create my table!






                                                              share|improve this answer

























                                                                up vote
                                                                2
                                                                down vote













                                                                Helpful tip, use SHOW WARNINGS; after trying your CREATE query and you will receive the error as well as the more detailed warning:



                                                                    ---------------------------------------------------------------------------------------------------------+
                                                                | Level | Code | Message |
                                                                +---------+------+-------------------------------------------------------------------------- -------------------------------------------------------------------------------------------- ---------------+
                                                                | Warning | 150 | Create table 'fakeDatabase/exampleTable' with foreign key constraint failed. There is no index in the referenced table where the referenced columns appear as the first columns.
                                                                |
                                                                | Error | 1005 | Can't create table 'exampleTable' (errno:150) |
                                                                +---------+------+-------------------------------------------------------------------------- -------------------------------------------------------------------------------------------- ---------------+


                                                                So in this case, time to re-create my table!






                                                                share|improve this answer























                                                                  up vote
                                                                  2
                                                                  down vote










                                                                  up vote
                                                                  2
                                                                  down vote









                                                                  Helpful tip, use SHOW WARNINGS; after trying your CREATE query and you will receive the error as well as the more detailed warning:



                                                                      ---------------------------------------------------------------------------------------------------------+
                                                                  | Level | Code | Message |
                                                                  +---------+------+-------------------------------------------------------------------------- -------------------------------------------------------------------------------------------- ---------------+
                                                                  | Warning | 150 | Create table 'fakeDatabase/exampleTable' with foreign key constraint failed. There is no index in the referenced table where the referenced columns appear as the first columns.
                                                                  |
                                                                  | Error | 1005 | Can't create table 'exampleTable' (errno:150) |
                                                                  +---------+------+-------------------------------------------------------------------------- -------------------------------------------------------------------------------------------- ---------------+


                                                                  So in this case, time to re-create my table!






                                                                  share|improve this answer












                                                                  Helpful tip, use SHOW WARNINGS; after trying your CREATE query and you will receive the error as well as the more detailed warning:



                                                                      ---------------------------------------------------------------------------------------------------------+
                                                                  | Level | Code | Message |
                                                                  +---------+------+-------------------------------------------------------------------------- -------------------------------------------------------------------------------------------- ---------------+
                                                                  | Warning | 150 | Create table 'fakeDatabase/exampleTable' with foreign key constraint failed. There is no index in the referenced table where the referenced columns appear as the first columns.
                                                                  |
                                                                  | Error | 1005 | Can't create table 'exampleTable' (errno:150) |
                                                                  +---------+------+-------------------------------------------------------------------------- -------------------------------------------------------------------------------------------- ---------------+


                                                                  So in this case, time to re-create my table!







                                                                  share|improve this answer












                                                                  share|improve this answer



                                                                  share|improve this answer










                                                                  answered Nov 27 '13 at 10:40









                                                                  sturrockad

                                                                  2,0611318




                                                                  2,0611318






















                                                                      up vote
                                                                      2
                                                                      down vote













                                                                      Please make sure at first that




                                                                      1. you are using InnoDB tables.

                                                                      2. field for FOREIGN KEY has the same type and length (!) as source field.


                                                                      I had the same trouble and I've fixed it. I had unsigned INT for one field and just integer for other field.






                                                                      share|improve this answer



























                                                                        up vote
                                                                        2
                                                                        down vote













                                                                        Please make sure at first that




                                                                        1. you are using InnoDB tables.

                                                                        2. field for FOREIGN KEY has the same type and length (!) as source field.


                                                                        I had the same trouble and I've fixed it. I had unsigned INT for one field and just integer for other field.






                                                                        share|improve this answer

























                                                                          up vote
                                                                          2
                                                                          down vote










                                                                          up vote
                                                                          2
                                                                          down vote









                                                                          Please make sure at first that




                                                                          1. you are using InnoDB tables.

                                                                          2. field for FOREIGN KEY has the same type and length (!) as source field.


                                                                          I had the same trouble and I've fixed it. I had unsigned INT for one field and just integer for other field.






                                                                          share|improve this answer














                                                                          Please make sure at first that




                                                                          1. you are using InnoDB tables.

                                                                          2. field for FOREIGN KEY has the same type and length (!) as source field.


                                                                          I had the same trouble and I've fixed it. I had unsigned INT for one field and just integer for other field.







                                                                          share|improve this answer














                                                                          share|improve this answer



                                                                          share|improve this answer








                                                                          edited Jul 29 '16 at 7:58









                                                                          Inzimam Tariq IT

                                                                          3,92562249




                                                                          3,92562249










                                                                          answered May 29 '16 at 13:25









                                                                          Juljan

                                                                          1,2181115




                                                                          1,2181115






















                                                                              up vote
                                                                              1
                                                                              down vote













                                                                              This is usually happening when you try to source file into existing database.
                                                                              Drop all the tables first (or the DB itself).
                                                                              And then source file with SET foreign_key_checks = 0; at the beginning and SET foreign_key_checks = 1; at the end.






                                                                              share|improve this answer

























                                                                                up vote
                                                                                1
                                                                                down vote













                                                                                This is usually happening when you try to source file into existing database.
                                                                                Drop all the tables first (or the DB itself).
                                                                                And then source file with SET foreign_key_checks = 0; at the beginning and SET foreign_key_checks = 1; at the end.






                                                                                share|improve this answer























                                                                                  up vote
                                                                                  1
                                                                                  down vote










                                                                                  up vote
                                                                                  1
                                                                                  down vote









                                                                                  This is usually happening when you try to source file into existing database.
                                                                                  Drop all the tables first (or the DB itself).
                                                                                  And then source file with SET foreign_key_checks = 0; at the beginning and SET foreign_key_checks = 1; at the end.






                                                                                  share|improve this answer












                                                                                  This is usually happening when you try to source file into existing database.
                                                                                  Drop all the tables first (or the DB itself).
                                                                                  And then source file with SET foreign_key_checks = 0; at the beginning and SET foreign_key_checks = 1; at the end.







                                                                                  share|improve this answer












                                                                                  share|improve this answer



                                                                                  share|improve this answer










                                                                                  answered Mar 27 '13 at 20:14









                                                                                  wholenewstrain

                                                                                  18416




                                                                                  18416






















                                                                                      up vote
                                                                                      1
                                                                                      down vote













                                                                                      I've found another reason this fails... case sensitive table names.



                                                                                      For this table definition



                                                                                      CREATE TABLE user (
                                                                                      userId int PRIMARY KEY AUTO_INCREMENT,
                                                                                      username varchar(30) NOT NULL
                                                                                      ) ENGINE=InnoDB;


                                                                                      This table definition works



                                                                                      CREATE TABLE product (
                                                                                      id int PRIMARY KEY AUTO_INCREMENT,
                                                                                      userId int,
                                                                                      FOREIGN KEY fkProductUser1(userId) REFERENCES **u**ser(userId)
                                                                                      ) ENGINE=InnoDB;


                                                                                      whereas this one fails



                                                                                      CREATE TABLE product (
                                                                                      id int PRIMARY KEY AUTO_INCREMENT,
                                                                                      userId int,
                                                                                      FOREIGN KEY fkProductUser1(userId) REFERENCES User(userId)
                                                                                      ) ENGINE=InnoDB;


                                                                                      The fact that it worked on Windows and failed on Unix took me a couple of hours to figure out. Hope that helps someone else.






                                                                                      share|improve this answer

























                                                                                        up vote
                                                                                        1
                                                                                        down vote













                                                                                        I've found another reason this fails... case sensitive table names.



                                                                                        For this table definition



                                                                                        CREATE TABLE user (
                                                                                        userId int PRIMARY KEY AUTO_INCREMENT,
                                                                                        username varchar(30) NOT NULL
                                                                                        ) ENGINE=InnoDB;


                                                                                        This table definition works



                                                                                        CREATE TABLE product (
                                                                                        id int PRIMARY KEY AUTO_INCREMENT,
                                                                                        userId int,
                                                                                        FOREIGN KEY fkProductUser1(userId) REFERENCES **u**ser(userId)
                                                                                        ) ENGINE=InnoDB;


                                                                                        whereas this one fails



                                                                                        CREATE TABLE product (
                                                                                        id int PRIMARY KEY AUTO_INCREMENT,
                                                                                        userId int,
                                                                                        FOREIGN KEY fkProductUser1(userId) REFERENCES User(userId)
                                                                                        ) ENGINE=InnoDB;


                                                                                        The fact that it worked on Windows and failed on Unix took me a couple of hours to figure out. Hope that helps someone else.






                                                                                        share|improve this answer























                                                                                          up vote
                                                                                          1
                                                                                          down vote










                                                                                          up vote
                                                                                          1
                                                                                          down vote









                                                                                          I've found another reason this fails... case sensitive table names.



                                                                                          For this table definition



                                                                                          CREATE TABLE user (
                                                                                          userId int PRIMARY KEY AUTO_INCREMENT,
                                                                                          username varchar(30) NOT NULL
                                                                                          ) ENGINE=InnoDB;


                                                                                          This table definition works



                                                                                          CREATE TABLE product (
                                                                                          id int PRIMARY KEY AUTO_INCREMENT,
                                                                                          userId int,
                                                                                          FOREIGN KEY fkProductUser1(userId) REFERENCES **u**ser(userId)
                                                                                          ) ENGINE=InnoDB;


                                                                                          whereas this one fails



                                                                                          CREATE TABLE product (
                                                                                          id int PRIMARY KEY AUTO_INCREMENT,
                                                                                          userId int,
                                                                                          FOREIGN KEY fkProductUser1(userId) REFERENCES User(userId)
                                                                                          ) ENGINE=InnoDB;


                                                                                          The fact that it worked on Windows and failed on Unix took me a couple of hours to figure out. Hope that helps someone else.






                                                                                          share|improve this answer












                                                                                          I've found another reason this fails... case sensitive table names.



                                                                                          For this table definition



                                                                                          CREATE TABLE user (
                                                                                          userId int PRIMARY KEY AUTO_INCREMENT,
                                                                                          username varchar(30) NOT NULL
                                                                                          ) ENGINE=InnoDB;


                                                                                          This table definition works



                                                                                          CREATE TABLE product (
                                                                                          id int PRIMARY KEY AUTO_INCREMENT,
                                                                                          userId int,
                                                                                          FOREIGN KEY fkProductUser1(userId) REFERENCES **u**ser(userId)
                                                                                          ) ENGINE=InnoDB;


                                                                                          whereas this one fails



                                                                                          CREATE TABLE product (
                                                                                          id int PRIMARY KEY AUTO_INCREMENT,
                                                                                          userId int,
                                                                                          FOREIGN KEY fkProductUser1(userId) REFERENCES User(userId)
                                                                                          ) ENGINE=InnoDB;


                                                                                          The fact that it worked on Windows and failed on Unix took me a couple of hours to figure out. Hope that helps someone else.







                                                                                          share|improve this answer












                                                                                          share|improve this answer



                                                                                          share|improve this answer










                                                                                          answered Aug 2 '14 at 5:54









                                                                                          Tim

                                                                                          191113




                                                                                          191113






















                                                                                              up vote
                                                                                              1
                                                                                              down vote













                                                                                              MySQL Workbench 6.3 for Mac OS.



                                                                                              Problem: errno 150 on table X when trying to do Forward Engineering on a DB diagram, 20 out of 21 succeeded, 1 failed. If FKs on table X were deleted, the error moved to a different table that wasn't failing before.



                                                                                              Changed all tables engine to myISAM and it worked just fine.



                                                                                              enter image description here






                                                                                              share|improve this answer



























                                                                                                up vote
                                                                                                1
                                                                                                down vote













                                                                                                MySQL Workbench 6.3 for Mac OS.



                                                                                                Problem: errno 150 on table X when trying to do Forward Engineering on a DB diagram, 20 out of 21 succeeded, 1 failed. If FKs on table X were deleted, the error moved to a different table that wasn't failing before.



                                                                                                Changed all tables engine to myISAM and it worked just fine.



                                                                                                enter image description here






                                                                                                share|improve this answer

























                                                                                                  up vote
                                                                                                  1
                                                                                                  down vote










                                                                                                  up vote
                                                                                                  1
                                                                                                  down vote









                                                                                                  MySQL Workbench 6.3 for Mac OS.



                                                                                                  Problem: errno 150 on table X when trying to do Forward Engineering on a DB diagram, 20 out of 21 succeeded, 1 failed. If FKs on table X were deleted, the error moved to a different table that wasn't failing before.



                                                                                                  Changed all tables engine to myISAM and it worked just fine.



                                                                                                  enter image description here






                                                                                                  share|improve this answer














                                                                                                  MySQL Workbench 6.3 for Mac OS.



                                                                                                  Problem: errno 150 on table X when trying to do Forward Engineering on a DB diagram, 20 out of 21 succeeded, 1 failed. If FKs on table X were deleted, the error moved to a different table that wasn't failing before.



                                                                                                  Changed all tables engine to myISAM and it worked just fine.



                                                                                                  enter image description here







                                                                                                  share|improve this answer














                                                                                                  share|improve this answer



                                                                                                  share|improve this answer








                                                                                                  edited Aug 24 '15 at 4:49

























                                                                                                  answered Aug 24 '15 at 4:44









                                                                                                  Eduardo Chongkan

                                                                                                  58059




                                                                                                  58059






















                                                                                                      up vote
                                                                                                      0
                                                                                                      down vote













                                                                                                      Also worth checking that you aren't accidentally operating on the wrong database. This error will occur if the foreign table does not exist. Why does MySQL have to be so cryptic?






                                                                                                      share|improve this answer

























                                                                                                        up vote
                                                                                                        0
                                                                                                        down vote













                                                                                                        Also worth checking that you aren't accidentally operating on the wrong database. This error will occur if the foreign table does not exist. Why does MySQL have to be so cryptic?






                                                                                                        share|improve this answer























                                                                                                          up vote
                                                                                                          0
                                                                                                          down vote










                                                                                                          up vote
                                                                                                          0
                                                                                                          down vote









                                                                                                          Also worth checking that you aren't accidentally operating on the wrong database. This error will occur if the foreign table does not exist. Why does MySQL have to be so cryptic?






                                                                                                          share|improve this answer












                                                                                                          Also worth checking that you aren't accidentally operating on the wrong database. This error will occur if the foreign table does not exist. Why does MySQL have to be so cryptic?







                                                                                                          share|improve this answer












                                                                                                          share|improve this answer



                                                                                                          share|improve this answer










                                                                                                          answered Jan 22 '13 at 14:27









                                                                                                          SystemParadox

                                                                                                          4,67333145




                                                                                                          4,67333145






















                                                                                                              up vote
                                                                                                              0
                                                                                                              down vote













                                                                                                              Make sure that the foreign keys are not listed as unique in the parent. I had this same problem and I solved it by demarcating it as not unique.






                                                                                                              share|improve this answer

























                                                                                                                up vote
                                                                                                                0
                                                                                                                down vote













                                                                                                                Make sure that the foreign keys are not listed as unique in the parent. I had this same problem and I solved it by demarcating it as not unique.






                                                                                                                share|improve this answer























                                                                                                                  up vote
                                                                                                                  0
                                                                                                                  down vote










                                                                                                                  up vote
                                                                                                                  0
                                                                                                                  down vote









                                                                                                                  Make sure that the foreign keys are not listed as unique in the parent. I had this same problem and I solved it by demarcating it as not unique.






                                                                                                                  share|improve this answer












                                                                                                                  Make sure that the foreign keys are not listed as unique in the parent. I had this same problem and I solved it by demarcating it as not unique.







                                                                                                                  share|improve this answer












                                                                                                                  share|improve this answer



                                                                                                                  share|improve this answer










                                                                                                                  answered Jun 4 '13 at 15:21









                                                                                                                  Raza

                                                                                                                  396614




                                                                                                                  396614






















                                                                                                                      up vote
                                                                                                                      0
                                                                                                                      down vote













                                                                                                                      In my case it was due to the fact that the field that was a foreign key field had a too long name, ie. foreign key (some_other_table_with_long_name_id). Try sth shorter. Error message is a bit misleading in that case.



                                                                                                                      Also, as @Jon mentioned earlier - field definitions have to be the same (watch out for unsigned subtype).






                                                                                                                      share|improve this answer

























                                                                                                                        up vote
                                                                                                                        0
                                                                                                                        down vote













                                                                                                                        In my case it was due to the fact that the field that was a foreign key field had a too long name, ie. foreign key (some_other_table_with_long_name_id). Try sth shorter. Error message is a bit misleading in that case.



                                                                                                                        Also, as @Jon mentioned earlier - field definitions have to be the same (watch out for unsigned subtype).






                                                                                                                        share|improve this answer























                                                                                                                          up vote
                                                                                                                          0
                                                                                                                          down vote










                                                                                                                          up vote
                                                                                                                          0
                                                                                                                          down vote









                                                                                                                          In my case it was due to the fact that the field that was a foreign key field had a too long name, ie. foreign key (some_other_table_with_long_name_id). Try sth shorter. Error message is a bit misleading in that case.



                                                                                                                          Also, as @Jon mentioned earlier - field definitions have to be the same (watch out for unsigned subtype).






                                                                                                                          share|improve this answer












                                                                                                                          In my case it was due to the fact that the field that was a foreign key field had a too long name, ie. foreign key (some_other_table_with_long_name_id). Try sth shorter. Error message is a bit misleading in that case.



                                                                                                                          Also, as @Jon mentioned earlier - field definitions have to be the same (watch out for unsigned subtype).







                                                                                                                          share|improve this answer












                                                                                                                          share|improve this answer



                                                                                                                          share|improve this answer










                                                                                                                          answered Oct 7 '14 at 12:48









                                                                                                                          Kangur

                                                                                                                          5,91431824




                                                                                                                          5,91431824






















                                                                                                                              up vote
                                                                                                                              0
                                                                                                                              down vote













                                                                                                                              When the foraign key constraint is based on varchar type, then in addition to the list provided by marv-el the target column must have an unique constraint.






                                                                                                                              share|improve this answer



























                                                                                                                                up vote
                                                                                                                                0
                                                                                                                                down vote













                                                                                                                                When the foraign key constraint is based on varchar type, then in addition to the list provided by marv-el the target column must have an unique constraint.






                                                                                                                                share|improve this answer

























                                                                                                                                  up vote
                                                                                                                                  0
                                                                                                                                  down vote










                                                                                                                                  up vote
                                                                                                                                  0
                                                                                                                                  down vote









                                                                                                                                  When the foraign key constraint is based on varchar type, then in addition to the list provided by marv-el the target column must have an unique constraint.






                                                                                                                                  share|improve this answer














                                                                                                                                  When the foraign key constraint is based on varchar type, then in addition to the list provided by marv-el the target column must have an unique constraint.







                                                                                                                                  share|improve this answer














                                                                                                                                  share|improve this answer



                                                                                                                                  share|improve this answer








                                                                                                                                  edited May 23 '17 at 12:03









                                                                                                                                  Community

                                                                                                                                  11




                                                                                                                                  11










                                                                                                                                  answered May 14 '15 at 14:42









                                                                                                                                  Ralph

                                                                                                                                  89.2k39226330




                                                                                                                                  89.2k39226330






















                                                                                                                                      up vote
                                                                                                                                      0
                                                                                                                                      down vote













                                                                                                                                      (Side notes too big for a Comment)



                                                                                                                                      There is no need for an AUTO_INCREMENT id in a mapping table; get rid of it.



                                                                                                                                      Change the PRIMARY KEY to (role_id, role_group_id) (in either order). This will make accesses faster.



                                                                                                                                      Since you probably want to map both directions, also add an INDEX with those two columns in the opposite order. (There is no need to make it UNIQUE.)



                                                                                                                                      More tips: http://mysql.rjweb.org/doc.php/index_cookbook_mysql#speeding_up_wp_postmeta






                                                                                                                                      share|improve this answer

























                                                                                                                                        up vote
                                                                                                                                        0
                                                                                                                                        down vote













                                                                                                                                        (Side notes too big for a Comment)



                                                                                                                                        There is no need for an AUTO_INCREMENT id in a mapping table; get rid of it.



                                                                                                                                        Change the PRIMARY KEY to (role_id, role_group_id) (in either order). This will make accesses faster.



                                                                                                                                        Since you probably want to map both directions, also add an INDEX with those two columns in the opposite order. (There is no need to make it UNIQUE.)



                                                                                                                                        More tips: http://mysql.rjweb.org/doc.php/index_cookbook_mysql#speeding_up_wp_postmeta






                                                                                                                                        share|improve this answer























                                                                                                                                          up vote
                                                                                                                                          0
                                                                                                                                          down vote










                                                                                                                                          up vote
                                                                                                                                          0
                                                                                                                                          down vote









                                                                                                                                          (Side notes too big for a Comment)



                                                                                                                                          There is no need for an AUTO_INCREMENT id in a mapping table; get rid of it.



                                                                                                                                          Change the PRIMARY KEY to (role_id, role_group_id) (in either order). This will make accesses faster.



                                                                                                                                          Since you probably want to map both directions, also add an INDEX with those two columns in the opposite order. (There is no need to make it UNIQUE.)



                                                                                                                                          More tips: http://mysql.rjweb.org/doc.php/index_cookbook_mysql#speeding_up_wp_postmeta






                                                                                                                                          share|improve this answer












                                                                                                                                          (Side notes too big for a Comment)



                                                                                                                                          There is no need for an AUTO_INCREMENT id in a mapping table; get rid of it.



                                                                                                                                          Change the PRIMARY KEY to (role_id, role_group_id) (in either order). This will make accesses faster.



                                                                                                                                          Since you probably want to map both directions, also add an INDEX with those two columns in the opposite order. (There is no need to make it UNIQUE.)



                                                                                                                                          More tips: http://mysql.rjweb.org/doc.php/index_cookbook_mysql#speeding_up_wp_postmeta







                                                                                                                                          share|improve this answer












                                                                                                                                          share|improve this answer



                                                                                                                                          share|improve this answer










                                                                                                                                          answered Feb 14 at 3:00









                                                                                                                                          Rick James

                                                                                                                                          64.2k55593




                                                                                                                                          64.2k55593






















                                                                                                                                              up vote
                                                                                                                                              -1
                                                                                                                                              down vote













                                                                                                                                              I encountered the same problem, but I check find that I hadn't the parent table. So I just edit the parent migration in front of the child migration. Just do it.






                                                                                                                                              share|improve this answer





















                                                                                                                                              • this should have been a comment instead of an answer
                                                                                                                                                – hannad rehman
                                                                                                                                                Jun 30 at 6:36















                                                                                                                                              up vote
                                                                                                                                              -1
                                                                                                                                              down vote













                                                                                                                                              I encountered the same problem, but I check find that I hadn't the parent table. So I just edit the parent migration in front of the child migration. Just do it.






                                                                                                                                              share|improve this answer





















                                                                                                                                              • this should have been a comment instead of an answer
                                                                                                                                                – hannad rehman
                                                                                                                                                Jun 30 at 6:36













                                                                                                                                              up vote
                                                                                                                                              -1
                                                                                                                                              down vote










                                                                                                                                              up vote
                                                                                                                                              -1
                                                                                                                                              down vote









                                                                                                                                              I encountered the same problem, but I check find that I hadn't the parent table. So I just edit the parent migration in front of the child migration. Just do it.






                                                                                                                                              share|improve this answer












                                                                                                                                              I encountered the same problem, but I check find that I hadn't the parent table. So I just edit the parent migration in front of the child migration. Just do it.







                                                                                                                                              share|improve this answer












                                                                                                                                              share|improve this answer



                                                                                                                                              share|improve this answer










                                                                                                                                              answered Jun 30 at 6:02









                                                                                                                                              韩向飞

                                                                                                                                              244




                                                                                                                                              244












                                                                                                                                              • this should have been a comment instead of an answer
                                                                                                                                                – hannad rehman
                                                                                                                                                Jun 30 at 6:36


















                                                                                                                                              • this should have been a comment instead of an answer
                                                                                                                                                – hannad rehman
                                                                                                                                                Jun 30 at 6:36
















                                                                                                                                              this should have been a comment instead of an answer
                                                                                                                                              – hannad rehman
                                                                                                                                              Jun 30 at 6:36




                                                                                                                                              this should have been a comment instead of an answer
                                                                                                                                              – hannad rehman
                                                                                                                                              Jun 30 at 6:36





                                                                                                                                              protected by Kermit Jul 10 '14 at 23:11



                                                                                                                                              Thank you for your interest in this question.
                                                                                                                                              Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                                                                                                                                              Would you like to answer one of these unanswered questions instead?



                                                                                                                                              Popular posts from this blog

                                                                                                                                              Costa Masnaga

                                                                                                                                              Fotorealismo

                                                                                                                                              Sidney Franklin