question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improve error messages for missing back-relation field

See original GitHub issue

With this Prisma schema:

model Event {
  id        Int      @id @default(autoincrement())
  owner     Person   @relation(name: "EventOwners")
  recipient Person?  @relation(name: "EventRecipients")
}

model Person {
  id        Int      @id @default(autoincrement())

}

I get this error VS Code:

Error validating model "Event": Automatic related field generation would cause a naming conflict. Please add an explicit opposite relation field.

image

The solution here is fairly straightforward (i.e. adding back-relation fields) but it’s not clear from the error message itself.

An improved error would be a bit more actionable, e.g.:

Please add the opposing relation field on the other model. 

Or even (if possibl):

Please add the opposing relation field on the `Person` model. 

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

26reactions
nikolasburkcommented, Apr 15, 2020

Hey @keepforever, Prisma always requires both relation fields to be present per relation. Since you want to create two relations, you need to have two relation fields on each side:

model User {
  id    Int     @default(autoincrement()) @id
  email String  @unique
  name  String?
  head  Note[]  @relation("head")
  torso Note[]  @relation("torso")
}

model Note {
  id        Int    @default(autoincrement()) @id
  title     String
  content   String
  userTorso User   @relation("head", fields: [userId], references: [id])
  userHead  User   @relation("torso", fields: [userId], references: [id])
  userId    Int
}

You can find more info in the docs.

2reactions
richardwardzacommented, Apr 16, 2020

To finish this off, my final solution is to reverse the references and store the id on the User tables and not the Image table:

model User {
  id         Int    @default(autoincrement()) @id
  name       String
  profilePic Image?  @relation("ProfilePicture", fields: [profileImageId], references: [id])
  avatar     Image?  @relation("AvatarImage", fields: [avatarImageId], references: [id])
  profileImageId  Int? @map("userProfilePic")
  avatarImageId   Int? @map("userAvatar")
}

model Image {
  id             Int    @default(autoincrement()) @id
  name           String
  userProfilePic User? @relation("ProfilePicture")
  userAvatar     User? @relation("AvatarImage")
 }

My database looks a lot saner:

CREATE TABLE "play"."User" (
  "id" int4 NOT NULL DEFAULT nextval('"play"."User_id_seq"'::regclass),
  "name" text COLLATE "pg_catalog"."default" NOT NULL,
  "userAvatar" int4,
  "userProfilePic" int4,
  CONSTRAINT "User_pkey" PRIMARY KEY ("id"),
  CONSTRAINT "User_userAvatar_fkey" FOREIGN KEY ("userAvatar") REFERENCES "play"."Image" ("id") ON DELETE SET NULL ON UPDATE CASCADE,
  CONSTRAINT "User_userProfilePic_fkey" FOREIGN KEY ("userProfilePic") REFERENCES "play"."Image" ("id") ON DELETE SET NULL ON UPDATE CASCADE
);

CREATE UNIQUE INDEX "User_userAvatar" ON "play"."User" USING btree (
  "userAvatar" "pg_catalog"."int4_ops" ASC NULLS LAST
);

CREATE UNIQUE INDEX "User_userProfilePic" ON "play"."User" USING btree (
  "userProfilePic" "pg_catalog"."int4_ops" ASC NULLS LAST
);

CREATE TABLE "play"."Image" (
  "id" int4 NOT NULL DEFAULT nextval('"play"."Image_id_seq"'::regclass),
  "name" text COLLATE "pg_catalog"."default" NOT NULL,
  CONSTRAINT "Image_pkey" PRIMARY KEY ("id")
);

My query:

const user = await prisma.user.create({
    data: {
        name: "user 1",
        profilePic: { create: { name: "profile pic 1" } },
        avatar: { create: { name: "avatar pic" } }
    },
    include: {
        profilePic: true,
        avatar: true
    }
})

returns:

user:  {
  id: 1,
  name: 'user 1',
  profileImageId: 1,
  avatarImageId: 2,
  profilePic: { id: 1, name: 'profile pic 1' },
  avatar: { id: 2, name: 'avatar pic' }
}

This now looks like the ramblings of a madman but leaving them here to hopefully help anyone coming after.

Read more comments on GitHub >

github_iconTop Results From Across the Web

django - Display validation errors in the same style as cripsy ...
My problem is that this error message displays at the top of the page as a flash message, while other messages (which i...
Read more >
How to Report Errors in Forms: 10 Design Guidelines
1. Aim for Inline Validation Whenever Possible · 2. Indicate Successful Entry for Complex Fields · 3. Keep Error Messages Next to Fields...
Read more >
Documentation: 15: 43.9. Errors and Messages - PostgreSQL
Use the RAISE statement to report messages and raise errors. ... If no condition name nor SQLSTATE is specified in a RAISE EXCEPTION...
Read more >
Error Messages | Help - Zoho Deluge
The TO field is included in the sendsms task. (Line no: 1) Missing return statement: Provide MAP expression to return. This error message...
Read more >
Error Messages: Examples, Best Practices & Common Mistakes
Useful error messages can keep users on your site and increase ... As she said, “the error could be missed or the fields...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found