MySQL “ERROR 1005 (HY000): Can't create table 'foo.#sql-12c_4' (errno: 150)”

后端 未结 14 783
情深已故
情深已故 2020-12-03 04:13

I was working on creating some tables in database foo, but every time I end up with errno 150 regarding the foreign key. Firstly, here\'s my code for creating t

相关标签:
14条回答
  • 2020-12-03 04:41

    Also both the tables need to have same character set.

    for e.g.

    CREATE TABLE1 (
      FIELD1 VARCHAR(100) NOT NULL PRIMARY KEY,
      FIELD2 VARCHAR(100) NOT NULL
    )ENGINE=INNODB CHARACTER SET utf8 COLLATE utf8_bin;
    

    to

    CREATE TABLE2 (
      Field3 varchar(64) NOT NULL PRIMARY KEY,
      Field4 varchar(64) NOT NULL,
      CONSTRAINT FORIGEN KEY (Field3) REFERENCES TABLE1(FIELD1)
    ) ENGINE=InnoDB;
    

    Will fail because they have different charsets. This is another subtle failure where mysql returns same error.

    0 讨论(0)
  • 2020-12-03 04:43

    Note: I had the same problem, and it was because the referenced field was in a different collation in the 2 different tables (they had exact same type).

    Make sure all your referenced fields have the same type AND the same collation!

    0 讨论(0)
  • 2020-12-03 04:45

    Subtle, but this error got me because I forgot to declare a smallint column as unsigned to match the referenced, existing table which was "smallint unsigned." Having one unsigned and one not unsigned caused MySQL to prevent the foreign key from being created on the new table.

    id smallint(3) not null
    

    does not match, for the sake of foreign keys,

    id smallint(3) unsigned not null
    
    0 讨论(0)
  • 2020-12-03 04:45

    I also received this error (for several tables) along with constraint errors and MySQL connecting and disconnecting when attempting to import an entire database (~800 MB). My issue was the result of The MySQL server max allowed packets being too low. To resolve this (on a Mac):

    • Opened /private/etc/my.conf
    • Under # The MySQL server, changed max_allowed_packet from 1M to 4M (You may need to experiment with this value.)
    • Restarted MySQL

    The database imported successfully after that.

    Note I am running MySQL 5.5.12 for Mac OS X (x86 64 bit).

    0 讨论(0)
  • 2020-12-03 04:46

    all, I solved a problem and wanted to share it:

    I had this error <> The issue was in that in my statement:

    alter table system_registro_de_modificacion add foreign key (usuariomodificador_id) REFERENCES Usuario(id) On delete restrict;

    I had incorrectly written the CASING: it works in Windows WAMP, but in Linux MySQL it is more strict with the CASING, so writting "Usuario" instead of "usuario" (exact casing), generated the error, and was corrected simply changing the casing.

    0 讨论(0)
  • 2020-12-03 04:55

    I got this completely worthless and uninformative error when I tried to:

    ALTER TABLE `comments` ADD CONSTRAINT FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE SET NULL ON UPDATE CASCADE;
    

    My problem was in my comments table, user_id was defined as:

    `user_id` int(10) unsigned NOT NULL
    

    So... in my case, the problem was with the conflict between NOT NULL, and ON DELETE SET NULL.

    0 讨论(0)
提交回复
热议问题