Rails 迁移问题 - 外键未创建
所以我有以下迁移 - 典型的评级表,它捕获评论的评级(值)。我有一些主键(pk),外键(fk),索引(index)的辅助方法,这一切都很好。
所以一切运行正常,但我注意到的是,comment_id 上的外键没有被创建,即使这个语句没有报告错误。任何帮助将不胜感激。谢谢。
执行(“更改表评级添加 约束 fk_ ratings_comment_id 外键(comment_id)参考 删除级联时的答案(id)”)
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
So I have the following migration - typical ratings table which captures ratings(value) for comments. I have some helper methods for primary-key(pk), foreign-key(fk), index(index) which is all fine.
So everything runs fine, but what i notice is that the foregn key on comment_id does not get created, even though this statement is not reporting an error. Any help would be appreciated. Thanks.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请尝试此操作,而不是使用“t.integer :comment_id, :null => false”
t.references :table_name,:options
Please try this instead of using "t.integer :comment_id, :null => false"
t.references :table_name,:options