2015-11-03 23 views
13

Tôi tạo ra một bảng bằng cách sử dụng chuyển đổi sau:Làm cách nào để loại bỏ ràng buộc duy nhất khỏi cột cơ sở dữ liệu trong Rails?

class CreateProfilePictures < ActiveRecord::Migration 
    def change 
    create_table :profile_pictures do |t| 
     t.integer :user_id, null: false 
     t.integer :picture_id, null: false 
     t.timestamps null: false 
    end 

    add_index :profile_pictures, :user_id, unique: true 
    add_index :profile_pictures, :picture_id, unique: true 
    end 
end 

tôi đã cố gắng để loại bỏ các hạn chế như sau:

class FixProfilePic < ActiveRecord::Migration 
    def change 
    change_column :profile_pictures, :picture_id, :integer, unique: false 
    end 
end 

tôi vẫn nhận được một lỗi vi phạm hạn chế duy nhất nếu tôi cố gắng sử dụng cùng một picture_id ở nhiều nơi. Cách thích hợp để loại bỏ ràng buộc duy nhất khỏi picture_id là gì?

+0

Bạn cần phải loại bỏ các 'index'. – Pavan

Trả lời

25

Bạn phải loại bỏ chỉ số của bạn với:

remove_index :profile_pictures, :picture_id 

add thêm nó một lần nữa với:

add_index :profile_pictures, :picture_id 

ActiveRecord::Migration

6

add_index: profile_pictures,: picture_id, độc đáo: true

Vì vậy, cập nhật chỉ số của bạn để:

remove_index :profile_pictures, :picture_id 
    add_index :profile_pictures, :picture_id 

Tôi đoán đây là nó.

Các vấn đề liên quan