Kinara Nyakaru
Kinara Nyakaru

Reputation: 501

Is it possible to create two or more relations to a model in prisma?

I'm trying to create two relations to a model in datamodel.prisma

datamodel.prisma

type User {
  id: ID! @id 
  user_id: String! @unique
  username: String!
  email: String! @unique
}

type Operation {
  id: ID! @id
  teams: [User] @relation(link: INLINE)
  created_by: User @relation(link: INLINE)
}

When I try to deploy this is the error I'm getting

Error

Errors:

  Operation
    ✖ The relation field `teams` must specify a `@relation` directive: `@relation(name: "MyRelation")`
    ✖ The relation field `created_by` must specify a `@relation` directive: `@relation(name: "MyRelation")`

What I want to achieve is an operation can have multiple members(one to many) and can only be created by one member(one to one). How can I achieve this in Prisma?

Upvotes: 1

Views: 1878

Answers (1)

Ryan
Ryan

Reputation: 6327

Could you try creating it like this:

type User {
  id: ID! @id
  user_id: String! @unique
  username: String!
  email: String! @unique
}

type Operation {
  id: ID! @id
  teams: [User] @relation(name: "Teams", link: TABLE)
  created_by: User @relation(name: "Createdby", link: TABLE)
}

The name field is required while creating multiple relations to the same model. Also I'm assuming you are using Postgres.

Upvotes: 1

Related Questions