开发者

Rails migration issue - foreign key not getting created

开发者 https://www.devze.com 2023-01-08 12:52 出处:网络
So I have the following migration - typical ratings table which captures ratings(value) for 开发者_JAVA百科comments. I have some helper methods for primary-key(pk), foreign-key(fk), index(index) which

So I have the following migration - typical ratings table which captures ratings(value) for 开发者_JAVA百科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


Please try this instead of using "t.integer :comment_id, :null => false"

t.references :table_name,:options

0

精彩评论

暂无评论...
验证码 换一张
取 消