Indexes - Unique with multiple terms

Hi,

I have this index.

{
  name: "test_unique_multiple",
  unique: true,
  serialized: true,
  source: "Car",
  terms: [
    {
      field: ["data", "owner"]
    },
    {
      field: ["data", "test1"]
    },
    {
      field: ["data", "test2"]
    },
    {
      field: ["data", "test3"]
    }
  ]
}

And i want create two documents.

{
  "owner":Get(Ref(Collection("users"), "276477638613139986"))
  "test1": "1",
  "test2": "2",
  "test3": "3"
}
{
  "owner":Get(Ref(Collection("users"), "276477821214261778"))
  "test1": "1",
  "test2": "2",
  "test3": "3"
}

Two owners different as you see. but has error with ‘document is not unique’
Has anybody else experienced this?

Any help would be appreciated :grin:

Objects don’t actually get indexed. Since you are saving an object to data.owner, it is not being indexed, and only data.test1, data.tes2, data.test3 are being included.

Question: why are you Geting the user to save in the Car? Usually, you just save the Ref. E.g.

{
"owner": Ref(Collection("users"), "276477638613139986")
"test1": "1",
"test2": "2",
"test3": "3"
}
1 Like

understand. Thanks for answer.

I was just trying to avoid N + 1 requests and unique with reference.
Saving Ref not working the index unique (owner:ref, test1:‘ok’),so i trying with Get.

If you want to save a copy of the user data you can still do that. To make the unique index work, you can use a binding and use the binding as a term.

The binding would be a simple scalar field taken from the user ref, that is, the ID value, which can then be used as a unique identifier for the index.

See the docs on bindings

1 Like

Thanks @ptpaterson !
I hadn’t seen this section(binding) on fauna.docs

Cool! Hope you can get working how you want it :slight_smile: Of course, reply back if you still have more questions.

You can do some pretty cool things using bindings on indexes.

1 Like