Rails 迁移问题 - 外键未创建

发布于 2024-09-11 04:52:12 字数 780 浏览 0 评论 0原文

所以我有以下迁移 - 典型的评级表,它捕获评论的评级(值)。我有一些主键(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玩心态 2024-09-18 04:52:12

请尝试此操作,而不是使用“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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文