Can't create two or more relations to User (from / to) on Event

prisma

#1

Hey there,

I’m trying to create something like

type Event {
  from: User
  to: User
}

EDIT: see the end but might have found my answer

When I try to deploy, I get a

  Event
    ✖ The relation field `from` must specify a `@relation` directive: `@relation(name: "MyRelation")`
    ✖ The relation field `to` must specify a `@relation` directive: `@relation(name: "MyRelation")`

If I try to name them, like this

type Event {
  from: User @relation(name: "Event_FromUser")
  to: User @relation(name: "Event_ToUser")
}

I get a A relation directive with a name must appear exactly 2 times. Relation name: 'Event_FromUser'

So basically I’m stuck except if I create something like “from_event” and “to_event” on User :confused: Did I missed something?

EDIT:

By looking at the schema on airbnb Sample on User and Messages It looks like you need to clearly name the relations one by one


#2

That’s correct. You can leave out @relation for single relation field between two types. But as soon as there at least two relation fields between the same two types, you need to connect the fields on both sides of the relations explicitely.

E.g., this works without @relation:

type A {
  id: ID! @unique 
  name: String!
  bs: [B!]!
}

type B {
  id: ID! @unique
  age: Int!
  a: A
}

Now, when introducing a second relation field from A to B, we need to use @relation directives:

type A {
  id: ID! @unique 
  name: String!
  bs: [B!]! @relation(name: "OldRelation")
  newBs: B! @relation(name: "New Relation")
}

type B {
  id: ID! @unique
  age: Int!
  a: A @relation(name: "OldRelation")
}

Confusion about relations - when they are needed and when not?
#3

Why @relation(name: “Blah”) is required? why is not handled automatically?


#4

when you create a B you should also have a reference to A but this reference is not stored in current document(row, entity) instead it store the reference of created B in A document, on may part. so when you query A from B it then refer to A collection(table) and search for stored B in A. in A we have two field which store reference to B. so by @relation(name: “targetB”) you tell the prisma which filed it should search for B