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.

Many to Many self-relation seems to generate the wrong `*CreateWithout*Input`

See original GitHub issue

Bug description

I think the gist of the issue is that the following relationship on the Follows table generates a FollowsCreateWithoutFollowingInput and I don’t believe that makes sense. It should create a FollowsCreateWithoutFollowerInput since when a Person has people they’re following then they are the follower and we just don’t know who the Person that they’re following is, so the input should contain the value for the Person they’re following

How to reproduce

  1. Add these two models to schema.prisma
model Person {
  id         Int       @id @default(autoincrement())
  name       String?
  followers  Follows[] @relation("follower")
  following  Follows[] @relation("following")
}

model Follows {
  follower    Person @relation("follower", fields: [followerId], references: [id])
  followerId  Int
  following   Person @relation("following", fields: [followingId], references: [id])
  followingId Int

  @@id([followerId, followingId])
}
  1. Run the migration npx prisma migrate dev
  2. Manually add a Person to the db to use as the Person you will be following
  3. Run the below code to get the error
const person = await prisma.person.create({
    data: {
      following: {
        create: [
          { following: { connect: { id: 1 } } }
        ]
      }
    }
  });

You should see the error.

/usr/src/app/node_modules/@prisma/client/runtime/index.js:34755
     const error2 = new PrismaClientValidationError(renderErrorStr(validationCallsite));
                    ^
 
 PrismaClientValidationError: Unknown arg `following` in data.following.create.0.following for type FollowsCreateWithoutFollowingInput. Did you mean `follower`?
 Argument follower for data.following.create.0.follower is missing.
 
     at Object.validate (/usr/src/app/node_modules/@prisma/client/runtime/index.js:34755:20)
     at PrismaClient._executeRequest (/usr/src/app/node_modules/@prisma/client/runtime/index.js:39749:17)
     at consumer (/usr/src/app/node_modules/@prisma/client/runtime/index.js:39690:23)
     at /usr/src/app/node_modules/@prisma/client/runtime/index.js:39694:49
     at AsyncResource.runInAsyncScope (node:async_hooks:199:9)
     at PrismaClient._request (/usr/src/app/node_modules/@prisma/client/runtime/index.js:39694:27)
     at request (/usr/src/app/node_modules/@prisma/client/runtime/index.js:39799:77)
     at _callback (/usr/src/app/node_modules/@prisma/client/runtime/index.js:40007:14)
     at PrismaPromise.then (/usr/src/app/node_modules/@prisma/client/runtime/index.js:40014:23) {
     clientVersion: '3.7.0'
 }

Expected behavior

Since I’m trying to create a Person with people they’re following, I expect to input (connect) a Person that they’re following not who the follower is. The Person I’m trying to create is the follower of this following[] relationship.

When prisma generates types from the schema, for the relationship following[] it should create FollowsCreateWithoutFollowerInput instead of FollowsCreateWithoutFollowingInput.

More details about this issue can be found here:

  1. https://stackoverflow.com/questions/70520589/prisma-many-to-many-self-relation-error-due-to-relation-and-generated-type-inpu
  2. https://prisma.slack.com/archives/CA491RJH0/p1640787469302900

Prisma information

generator client {
  provider      = "prisma-client-js"
  binaryTargets = ["native", "linux-musl"]
}

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

model Person {
  id         Int       @id @default(autoincrement())
  name       String?
  followers  Follows[] @relation("follower")
  following  Follows[] @relation("following")
}

model Follows {
  follower    Person @relation("follower", fields: [followerId], references: [id])
  followerId  Int
  following   Person @relation("following", fields: [followingId], references: [id])
  followingId Int

  @@id([followerId, followingId])
}
const person = await prisma.person.create({
    data: {
      following: {
        create: [
          { following: { connect: { id: 1 } } }
        ]
      }
    }
  });

Environment & setup

  • OS: Docker node:16-alpine
  • Database: SQLServer
  • Node.js version: v16.13.1

Prisma Version

prisma                  : 3.7.0
@prisma/client          : 3.7.0
Current platform        : linux-musl
Query Engine (Node-API) : libquery-engine 8746e055198f517658c08a0c426c7eec87f5a85f (at node_modules/@prisma/engines/libquery_engine-linux-musl.so.node)
Migration Engine        : migration-engine-cli 8746e055198f517658c08a0c426c7eec87f5a85f (at node_modules/@prisma/engines/migration-engine-linux-musl)
Introspection Engine    : introspection-core 8746e055198f517658c08a0c426c7eec87f5a85f (at node_modules/@prisma/engines/introspection-engine-linux-musl)
Format Binary           : prisma-fmt 8746e055198f517658c08a0c426c7eec87f5a85f (at node_modules/@prisma/engines/prisma-fmt-linux-musl)
Default Engines Hash    : 8746e055198f517658c08a0c426c7eec87f5a85f
Studio                  : 0.445.0

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
renkencommented, Jan 22, 2022

See the explicit many-to-many example here.

1reaction
janpiocommented, Jan 11, 2022

@p-kuen Reproduction welcome as well of course 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Prisma many to many self relation error due to @relation and ...
So prisma.person.create some data that this person has a follower and that follower is connected to the user with id whatever.
Read more >
Self-relations - Prisma
To create a one-to-one self-relation: Both sides of the relation must define a @relation attribute that share the same name - in this...
Read more >
PSYC 101 UCSD Midterm 3 Flashcards | Quizlet
-make-believe activities in which children create new symbolic relations, ... -a fundamental but rare skill: many species show no awareness of self in...
Read more >
ClasSOm-Technigues - ERIC
Many researchers view creativity as a composite of intellectual ... make mistakes without fear of coercion, humiliation, and a feeling of total failure....
Read more >
Self-referential many-to-many relationships in gallery
Hi, I'm facing an issue with a self-referential many-to-many relationship. So I have an entity (let's call it entityA since I can't discuss ......
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