sql – 如何从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

我尝试删除以下的约束:

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

如果我尝试在多个地方使用相同的picture_id,我仍然会收到一个唯一的约束违规错误.从图片ID删除唯一性约束的正确方法是什么?

解决方法

您必须删除您的索引:
remove_index :profile_pictures,:picture_id

添加添加

add_index :profile_pictures,:picture_id

ActiveRecord::Migration

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 &#39;EastRiver&#39; 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...