我的MySQL數據庫中有一個用戶表。在我的軌道4應用程序,我創建了一個權限模型如下:運行rails遷移時無法添加外鍵約束錯誤
rails g model Permission user:references privilege:references
當我運行遷移用於創建權限表,該表不會獲取在數據庫中創建,但我得到了以下錯誤:
== 20170223171936 CreatePermissions: migrating ================================
-- create_table(:permissions)
rake aborted!
StandardError: An error has occurred:
Mysql2::Error: Cannot add foreign key constraint: ALTER TABLE `permissions` ADD CONSTRAINT `fk_rails_d9cfa3c257`
FOREIGN KEY (`user_id`)
REFERENCES `users` (`id`)
由於users表已經存在,所以我很疑惑爲什麼外鍵創建在權限表中失敗。這裏可能是什麼問題?