Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-1657

Uniqueness constraint violated when attribute doesn't change but scope changes.

    • Type: Icon: Task Task
    • Resolution: Done
    • 2.4.4
    • Affects Version/s: None
    • Component/s: None
    • None

      class Folder
        include Mongoid::Document
        field :name, :type => String
        has_many :folder_items
      end
      
      class FolderItem
        include Mongoid::Document
        belongs_to :folder
        field :name, :type => String
        validates :name, :uniqueness => {:scope => :folder_id} 
      end
      
      personal_folder.folder_items << FolderItem.new(:name => "non-unique") 
      public_folder.folder_items   << FolderItem.new(:name => "non-unique") 
      
      public_folder.folder_items.first.update_attributes(:folder_id => personal_folder.id)
      

      Last statement should fail, but it doesn't. It's because as per the MONGOID-1577 we skip db call if the attribute value hasn't changed. However this skips the fact that value may remain the same but scope may change; hence the failure.

            Assignee:
            Unassigned Unassigned
            Reporter:
            priyaaank priyaaank
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: