Removing a unique directive

Hi there,

I made a dumb mistake and put a @uniq directive on a type property, only to realize later that I don’t actually want that.

type Thing {
    id: ID!
    slug: String! @unique
    userId: String!
    title: String!
    createdAt: Int!
    updatedAt: Int!
}

I’ve read the docs and found this post, here in the forums, but I think I need some more clarity to get things working as I want them to.

In brief, no things can be created with the same slug.

I do have an index that was created when I added the directive. I deleted that index, and the problem remains.

Two Questions:

  1. How can I get rid of the current constraint?
  2. How can I add a new index with two terms (e.g. slug, userId) that would prevent a user from creating a document where the combination of slug and userId are the constraint.

For the later, userId would not necessarily be the “owner” (i.e. the person who created it) of the Thing document.

Any clarifications on this would be most appreciated.

  1. How can I get rid of the current constraint?

Edit your schema definition to remove @unique, and re-upload it via the Dashboard.

  1. How can I add a new index with two terms (e.g. slug, userId) that would prevent a user from creating a document where the combination of slug and userId are the constraint.

Use CreateIndex and specify in the terms definition the two fields required:

  terms: [
    { field: ["data", "slug"] },
    { field: ["data", "userId"] },
  ],

Also, make sure that unique: true is included in your index definition.

Then, re-introduce the @unique directive on the slug field, specifying the index that you created:

type Thing {
    id: ID!
    slug: String! @unique(index: "<name of your index>")
    userId: String!
    title: String!
    createdAt: Int!
    updatedAt: Int!
}
1 Like

Hi @ewan,

Thanks much for the very helpful reply.

I got stuff working with a little hitch that I’ll explain here, in case someone else comes across this.

My schema is in a schema.graphql file, so I did the following:

  1. Removed the unique directive on the slug field on type Thing
  2. Performed a merge using the VS Code extension (which is pretty nifty).
  3. Created a new index per your suggested settings.
  4. Re-introduced the directive in the schema.graphql

When I attempt to merge, I get the following:

Conflicts found in 'Index("unique_recipe_slug_and_user_id")':
  - 'terms' field cannot be updated from '[ { field: ["data", "slug"] }, { field: ["data", "userId"] } ]' to '[ { field: ["data", "slug"] } ]'. Once created, an Index cannot modified its 'terms' field.

I was baffled for a bit, as this kind of seems backward in that I should be “going to” using both terms, where the above seems to say I’m “going to” a single term. That said, I am easily baffled.

Then I did some testing…

I can now create things that have the same slug but with unique userIDs, which is exactly what I want.

It appears that I don’t need to re-introduce the unique directory in the schema.graphql file and merge it, as the creation of the the index in step #3 above, already took care of that.

Kindly confirm my understanding or correct me, if wrong, so that folks coming here might get the needed clarify.

If the index is providing the behavior that you need, then you’re good to go.

The @unique directive informs the definition of an auto-created index, but the behavior comes from the index itself. When you make an index with unique: true set, the GraphQL API has to abide by that.

I’m glad you got it working!

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.