# Dgraph fails to parse schema using @id directive

**URL:** <https://discuss.dgraph.io/t/dgraph-fails-to-parse-schema-using-id-directive/15145>\
**Category:** GraphQL\
**Tags:** kind:question\
**Created:** [July 30, 2021, 8:42pm UTC](https://discuss.dgraph.io/t/dgraph-fails-to-parse-schema-using-id-directive/15145 "2021-07-30T20:42:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nadroz](https://avatars.discourse-cdn.com/v4/letter/n/a3d4f5/32.png) [@nadroz](https://discuss.dgraph.io/u/nadroz)\
**Post date:** [July 30, 2021, 8:42pm UTC](https://discuss.dgraph.io/t/dgraph-fails-to-parse-schema-using-id-directive/15145/1 "2021-07-30T20:42:02Z")

</div>

I’m the following assumes:  
`dgraph version: 21.03.1`  
`dgo version: 210`

I’ve tagged this as a question because I’m not sure if it’s a bug, or user error. I have a schema for which I’d like to apply the `@id` directive to a predicate called `id`. I’d like that predicate to be required, and from what I’ve read, the `@id` directive should:

- ensure uniqeness
- provide a hash index

My use case matches what I’ve read regarding the purpose of the `@id` directive. I need to store and index external ids which must also have a uniqueness constraint on them.

In my experiments so far, I haven’t been able to successfully lay down schema that leverages the `@id` directive. When I try to apply the schema below (taken directly from this documentation post: [http://discuss.hypermode.com/t/ids-graphql/9766](http://discuss.hypermode.com/t/ids-graphql/9766)) via grpc using the dgo `Alter` call…

```auto
type User {
    username: string! @id .
}

```

I get this error: `rpc error: code = Unknown desc = line 3 column 22: Expected new line after field declaration. Got @`  
So in this case it seems like I can’t even apply the directive to the schema

My second experiment is as follows. In my schema, I define an id predicate as follows:  
`id: string @id .`  
Then, in the types where it’s required, I attempt to add the predicate as follows:

```auto
type Device {
		id
               ...
}

```

When I attempt to apply this version of the schema, I get the following error:  
`rpc error: code = Unknown desc = line 2 column 13: Invalid index specification`

So, what I can’t figure out here is if the schema parsing is just very picky, and I’m doing something wrong, or if somehow `@id` is not supported/no longer supported/never was supported. Any feedback on this would be greatly appreciated. Thanks in advance,

```
 -Nick
```

---

<div class="post-metadata">

**Author:** ![pshaddel](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/pshaddel/32/11584_2.png) [@pshaddel](https://discuss.dgraph.io/u/pshaddel)\
**Post date:** [July 31, 2021, 2:19pm UTC](https://discuss.dgraph.io/t/dgraph-fails-to-parse-schema-using-id-directive/15145/2 "2021-07-31T14:19:51Z")

</div>

Hello Nick and Welcome.  
An example of User schema:

```auto
type User {
  UserID: ID!
  username: String! @id
}

```

---

<div class="post-metadata">

**Author:** ![nadroz](https://avatars.discourse-cdn.com/v4/letter/n/a3d4f5/32.png) [@nadroz](https://discuss.dgraph.io/u/nadroz)\
**Post date:** [July 31, 2021, 3:41pm UTC](https://discuss.dgraph.io/t/dgraph-fails-to-parse-schema-using-id-directive/15145/3 "2021-07-31T15:41:49Z")

</div>

Hi Poorshad. Thanks. I’m not actually trying to set up a user schema. I’m using a direct example from dgraph’s documentation (here: [IDs - Graphql](http://discuss.hypermode.com/t/ids-graphql/9766)) to test out using the `@id` directive. I’ll try what you have provided as well, but I could use some more guidance on the `@id` directive specifically where using the `dgo` golang library to lay down a schema in a graphdb database.

---

<div class="post-metadata">

**Author:** ![nadroz](https://avatars.discourse-cdn.com/v4/letter/n/a3d4f5/32.png) [@nadroz](https://discuss.dgraph.io/u/nadroz)\
**Post date:** [July 31, 2021, 3:48pm UTC](https://discuss.dgraph.io/t/dgraph-fails-to-parse-schema-using-id-directive/15145/4 "2021-07-31T15:48:03Z")

</div>

> [@pshaddel](#):
>
> ```auto
> type User {
> UserID: ID!
> username: String! @id
> }
> 
> ```

Ok, as i mentioned below, my purpose here isn’t to create a user schema, but to figure out why the `@id` directive seems to cause trouble when I try to lay schema down using the `Alter` call of the `dgo` golang library to talk to dgraph. When I try to lay down exactly the type you have defined above, this is the result:  
`rpc error: code = Unknown desc = line 4 column 19: Expected new line after field declaration. Got @`

In the schema definition I made with the exact type definition you provided above, line four is `username: String! @id`. Which returns me to my main question. Am I doing something wrong or is it just not possible to use the @id directive when programmatically laying down schema?

---

<div class="post-metadata">

**Author:** ![pshaddel](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/pshaddel/32/11584_2.png) [@pshaddel](https://discuss.dgraph.io/u/pshaddel)\
**Post date:** [July 31, 2021, 4:51pm UTC](https://discuss.dgraph.io/t/dgraph-fails-to-parse-schema-using-id-directive/15145/5 "2021-07-31T16:51:47Z")

</div>

Sorry for misunderstanding, I think the schema in that link is a GraphQL schema and what you want to do is to change schema using Dgraph Go lang client. I’m not using dgo library but here is an example of using it:  
[https://pkg.go.dev/github.com/dgraph-io/dgo?utm\_source=godoc#example-package-GetSchema](https://pkg.go.dev/github.com/dgraph-io/dgo?utm_source=godoc#example-package-GetSchema)

Also after we use @id in GraphQL schema and load that graphql schema using CURL I saw that this is how DQL schema changed:

```auto
User.username: string @index(hash) @upsert .

```

The reason why predicate name is `User.username` because I’m using GraphQL schema and it uses type in first part of the predicate name.
