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.

`thread '<unnamed>' panicked at 'Did not find a relation for model users and field editedmessages'`

See original GitHub issue
generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["mongoDb", "fullTextSearch"]
}

datasource db {
  provider = "mongodb"
  url      = env("DATABASE_URL")
}

model editedmessages {
  id          String   @id @default(auto()) @map("_id") @db.ObjectId
  userId String
  oldMessage String
  newMessage String
  editDate String
  messageID String
  user   users   @relation("users", fields: [userId], references: [userId], onDelete: NoAction, onUpdate: NoAction)
  users  users[]
}

model stafftags {
  id          String   @id @default(auto()) @map("_id") @db.ObjectId
  v           Int      @map("__v")
  fullMessage String
  type        String[]
  userId      String
}

model statistics {
  id    String @id @default(auto()) @map("_id") @db.ObjectId
  v     Int    @map("__v")
  date  String
  total Int
  type  String
}

model users {
  id               String           @id @default(auto()) @map("_id") @db.ObjectId
  v                Int              @map("__v")
  avatarURL        String
  firstMessage     String
  lastMessage String?
  userId           String           @unique(map: "userId_1")
  username         String           @unique(map: "username_1")
  statistics       userstatistics   @relation(fields: [userId], references: [userId])
  userstatistics   userstatistics[] @relation("users")
  message       editedmessages   @relation(fields: [userId], references: [userId])
  editedmessages   editedmessages[] @relation("users")
}

model userstatistics {
  id     String  @id @default(auto()) @map("_id") @db.ObjectId
  v      Int     @map("__v")
  total  Int
  userId String
  user   users   @relation("users", fields: [userId], references: [userId], onDelete: NoAction, onUpdate: NoAction)
  users  users[]
}

model webusers {
  id       String  @id @default(auto()) @map("_id") @db.ObjectId
  username String
  password String
  email    String
  admin    Boolean
}

Throws an error,

❯ npx prisma format
Prisma schema loaded from prisma\schema.prisma
thread '<unnamed>' panicked at 'Did not find a relation for model users and field editedmessages', query-engine\prisma-models\src\builders\internal_dm_builder.rs:122:25
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Error: Unexpected token D in JSON at position 0

_Originally posted by @schwarzsky in https://github.com/prisma/prisma/issues/4854#issuecomment-1117845073_

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
janpiocommented, May 6, 2022

Here is a more minimal version of the schema that shows the same crash:

generator client {
  provider        = "prisma-client-js"
}

datasource db {
  provider = "mongodb"
  url      = env("DATABASE_URL")
}

model editedmessages {
  id         String  @id @default(auto()) @map("_id") @db.ObjectId
  userId     String
  user       users   @relation("users", fields: [userId], references: [userId])
}

model users {
  id             String           @id @default(auto()) @map("_id") @db.ObjectId
  userId         String           @unique
  userstatistics userstatistics[] @relation("users")
  editedmessages editedmessages[] @relation("users")
}

model userstatistics {
  id     String  @id @default(auto()) @map("_id") @db.ObjectId
  userId String
  user   users   @relation("users", fields: [userId], references: [userId])
}

The problem that seems to be causing this is that both relations are called users - changing 2 connected instances if users to users2 makes the schema pass validation:

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "mongodb"
  url      = env("DATABASE_URL")
}

model editedmessages {
  id     String @id @default(auto()) @map("_id") @db.ObjectId
  userId String
  user   users  @relation("users2", fields: [userId], references: [userId])
}

model users {
  id             String           @id @default(auto()) @map("_id") @db.ObjectId
  userId         String           @unique
  userstatistics userstatistics[] @relation("users")
  editedmessages editedmessages[] @relation("users2")
}

model userstatistics {
  id     String @id @default(auto()) @map("_id") @db.ObjectId
  userId String
  user   users  @relation("users", fields: [userId], references: [userId])
}

Was this schema created via Introspection @schwarzsky? It looks a bit like that. If so, can you open another issue and post a minimal sample of the data to create that schema via Introspection? That both relations get the same name is a bug that needs to be fixed as well.

1reaction
schwarzskycommented, May 6, 2022

Thanks. Any help apprecaited.

Read more comments on GitHub >

github_iconTop Results From Across the Web

This Week in Matrix 2022-12-02
Display names of Slack users should be kept up-to-date more reliably. Support is added for bridging Slack threads with MSC3440 m.thread relations.
Read more >
Querying models based on their relations existence
Problem. Common use cases when querying for relations from a database are: finding all the entities for table X where the relation Y...
Read more >
Mattermost self-hosted changelog
Fixed an issue where Get categories with the “exclude” option did not return ... Users are unable to open threads from recent mentions...
Read more >
Mozilla is giving up on their IRC server
Users reporting spam is done by them sending a message to an admin ... It is paid for by someone, but I don't...
Read more >
Unable to solve Prisma 1 - 1 and 1 - n relation error
Issue Resolved Update. After I do prisma migrate save , prisma migrate up and prisma generate , I stopped the server running via...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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