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.

relation for borth friend

See original GitHub issue

Bug description

firend table has two userid

How to reproduce

Run ‘export DEBUG=“*” && npx prisma migrate dev --preview-feature’ See error

Expected behavior

Error: Error: Database error: Error querying the database: Server error: `ERROR HY000 (1822): Failed to add the foreign key constraint. Missing index for constraint 'friends_ibfk_1' in the referenced table 'Users''
   0: migration_core::api::ApplyMigrations
             at migration-engine/core/src/api.rs:102

Prisma information

// This is your Prisma schema file, // learn more about it in the docs: https://pris.ly/d/prisma-schema

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

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

model Users {
  id        Int       @id @default(autoincrement())
  name      String
  createdAt DateTime  @default(now())
  updatedAt DateTime  @updatedAt
  Friends   Friends[]
}

model Friends {
  id           Int      @id @default(autoincrement())
  userId       Int
  friendUserId Int
  Users        Users    @relation(fields: [userId, friendUserId], references: [id, id])
  status       Boolean  @default(true)
  createdAt    DateTime @default(now())
  updatedAt    DateTime @updatedAt
}

Environment & setup

  • OS: macOS 10.15.7
  • Database: MySQL
  • Node.js version: v14.0.0
  • Prisma version:

@prisma/cli : 2.13.0 @prisma/client : 2.13.0

@prisma/cli : 2.13.1 @prisma/client : 2.13.1


Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
pantharshit00commented, Jan 21, 2021

The following model creates the required tables:

model Users {
  id                Int       @id @default(autoincrement())
  name              String
  createdAt         DateTime  @default(now())
  updatedAt         DateTime  @updatedAt
  userFriends       Friends[] @relation(name: "users")
  friendUserFriends Friends[] @relation(name: "friend_users")
}

model Friends {
  id        Int      @id @default(autoincrement())
  user_id   Int
  friend_id Int
  user      Users    @relation(name: "users", fields: [user_id], references: [id])
  friend    Users    @relation(name: "friend_users", fields: [friend_id], references: [id])
  status    Boolean  @default(true)
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  @@unique([user_id, friend_id])
}

You can also introspect(https://www.prisma.io/docs/concepts/components/introspection) the database if you are more comfortable in writing the schema in sql first.

0reactions
detronetdipcommented, Sep 12, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Support Your Friend Who Just Had a Baby
Some of this depends on your relationship with the person as well; know your friend might be sensitive about ANYONE holding his or...
Read more >
How To Go From "Friends" To "Officially Dating" - YouTube
Hi Youtube, I'm Amy North. I'm a relationship specialist and it's my mission to help women find the love they want and deserve....
Read more >
AITA for wanting to be a part of my best friends birth - Reddit
Your friend wants you there because she needs you and wants you to be part of her journey. It's for her to decide,...
Read more >
The Loneliness of Early Parenthood - The Atlantic
After all, “similarity breeds friendship by forming a basis for conversation and joint activities,” argue the Dutch researchers in their study.
Read more >
Friends: What Happened To Phoebe's Birth Mother
Phoebe Abbott was a close friend of Lily and Frank Buffay, Phoebe's father. The three were in a relationship which led to Phoebe...
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