Supports transactions, row-level locking, and foreign keys

后端 未结 1 437
天命终不由人
天命终不由人 2020-12-11 08:40

For some reason I cannot create this table:

CREATE TABLE user_role (
  user_id VARCHAR(20) NOT NULL,
  role_id INTEGER UNSIGNED NOT NULL,

  FOREIGN KEY (use         


        
相关标签:
1条回答
  • 2020-12-11 08:51

    See http://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html

    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 correct column names and types, and it must have indexes on the referenced keys, as stated earlier. If these are not satisfied, MySQL returns Error 1005 and refers to Error 150 in the error message, which means that a foreign key constraint was not correctly formed.

    If is rather difficult to guess as you didn't provide the definitions for the roles and permissions tables but to paraphrase the doc...

    • ...in order to have a foreign key on a column, you must have an index on the "target" column.
    • ...in order to have a foreign key on a column, both "source" and "target" columns must have the same type (incl. the same size if applicable).
    • ...in order to have a foreign key on a column, both tables must use InnoDB engine.
    0 讨论(0)
提交回复
热议问题