Error validating: This line is not a valid field or attribute definition

Viewed 1600

I wonder why my model (the Like-model) does not work as I expect it to.

Maybe someone can explain?

model User {
  id Int @id @default(autoincrement())
  likes Like[]
}

model Like {
  fromUser User @relation(fields: [fromUserId] references: [id])
  fromUserId Int
  toUser User @relation(fields: [toUserId] references: [id])
  toUserId Int
  @@id([fromUserId, toUserId])
}

The error reads: Error validating: This line is not a valid field or attribute definition.

It points at fromUser User @relation(fields: [fromUserId] references: [id]) and toUser User @relation(fields: [toUserId] references: [id]).

2 Answers

You would need to model your relations in the following manner:

model User {
  id            Int    @id @default(autoincrement())
  likedUsers    Like[] @relation("likedUsers")
  usersWhoLiked Like[] @relation("usersWhoLiked")
}

model Like {
  id             Int   @id @default(autoincrement())
  likedUser      User? @relation("likedUsers", fields: [likedUserId], references: [id])
  likedUserId    Int?
  userWhoLiked   User? @relation("usersWhoLiked", fields: [userWhoLikedId], references: [id])
  userWhoLikedId Int?
}

Whenever you have more than 1 relation to a model you need to provide a relation name to disambiguate the relation.

Also you need to store which users you have liked and who liked the current user. So you would need two relations for this.

Missing commas. The solution might look like this:

model User {
  id Int @id @default(autoincrement())
  likes Like[]
}

model Like {
  fromUser User @relation(fields: [fromUserId], references: [id])
  fromUserId Int
  toUser User @relation(fields: [toUserId], references: [id])
  toUserId Int
  @@id([fromUserId, toUserId])
}
Related