所以我有以下迁移 - 典型的评级表,它捕获评论的评级(值)。我有一些用于主键 (pk)、外键 (fk)、索引 (index) 的辅助方法,这些都很好。
所以一切运行良好,但我注意到 comment_id 上的外键没有创建,即使这个语句没有报告错误。任何帮助,将不胜感激。谢谢。
execute("ALTER TABLE ratings ADD CONSTRAINT fk_ratings_comment_id FOREIGN KEY (comment_id) REFERENCES answers(id) ON DELETE CASCADE")
class CreateRatings < ActiveRecord::Migration
extend MigrationHelpers
def self.up
create_table :ratings, :id => false do |t|
t.integer :comment_id, :null => false
t.integer :user_id, :null => false
t.integer :value, :null => false, :limit => 1
t.timestamps
end
pk :ratings, %w{ comment_id user_id }
fk :ratings, :comment_id, :comments, true
fk :ratings, :user_id, :users
index :ratings, %w{ comment_id value }
end
最佳答案
请试试这个而不是使用“t.integer :comment_id, :null => false”
t.references :table_name,:options
关于mysql - Rails 迁移问题 - 未创建外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3303216/