Jirico
Jirico

Reputation: 1262

Mongoid embedded model index not working

I have a company that contains products:

class Company
  include Mongoid::Document
  include Mongoid::Timestamps

  embeds_many :products

  index({"products.code" => 1}, {sparse: true, unique: true})

end

But when I create products(in the same parent) with the same code, like '123', the index does not check the uniqueness of it and it gets created normally. What is wrong?

Upvotes: 1

Views: 535

Answers (1)

Anand Jayabalan
Anand Jayabalan

Reputation: 12904

Unique indexes are not enforced in embedded documents. There's an open issue on this.

Also, check this page for some workarounds/approaches to enforce this.

Upvotes: 3

Related Questions