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
精彩评论