I am trying to rename relational fields as per the...
# orm-help
k
I am trying to rename relational fields as per the documentation but I'm getting an error that the "fields" attribute is required. Doesn't that defeat the purpose of rename relational fields cause otherwise annotated fields would have to be used? using v2.29.1
r
@KJReactor 👋 I didn’t quite get your point. Could you explain with an example?
k
@Ryan Using the same example given in the doc :
model Post {
id          Int   @default(autoincrement()) @id
author      User  @relation("Post_authorToUser", references: [id])
favoritedBy User? @relation("Post_favoritedByToUser", references: [id])
}
model User {
id              Int    @default(autoincrement()) @id
writtenPost     Post[] @relation("Post_authorToUser")
favoritedPosts  Post[] @relation("Post_favoritedByToUser")
}
I get the error that the fields argument is missing and required in the relation attribute
Therefore, I would be forced to do something like: this for posts:
model Post {
id          Int   @default(autoincrement()) @id
author      User  @relation("Post_authorToUser", references: [id])
favoritedBy Int
user User? @relation(fields: "favoritedBy" , references: [id])
}
r
Your schema should look like this:
Copy code
model Post {
  id            Int   @id @default(autoincrement())
  author        User  @relation("Post_authorToUser", references: [id], fields: [userId])
  favoritedBy   User? @relation("Post_favoritedByToUser", references: [id], fields: [favoritedById])
  userId        Int
  favoritedById Int?
}

model User {
  id             Int    @id @default(autoincrement())
  writtenPost    Post[] @relation("Post_authorToUser")
  favoritedPosts Post[] @relation("Post_favoritedByToUser")
}
The reason is that you are pointing to the same model twice via
writtenPost
and
favoritedPosts
so you need to provide the opposite relation with the foreign key twice as well.