views:

126

answers:

1

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
+1  A: 

Hi ,

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

t.references :table_name,:options

gsoni
but this wont create the constraints in the database, it will only create column definitions. Right?
Yes,this wont create any constraint,it will just add a column.But to apply constraints on database you need to interact with Model class.
gsoni