Skip to content

Multiple Relations from a table to another are not correctly introspected #3915

Open

Description

@diegozuber

Bug description

I have a table called "Group". A "Group" has a Many-to-Many relation to "User" and also a field called authorId which has a foreign key to "User.id" (One-to-Many Relation). On introspecting I generates the schema file, but if I wanna use it, I always get the error "P2021 - The table _GroupToUserManyToMany does not exist in the current database.".

Here the schema of the product:

model Group {
  id             Int          @id @default(autoincrement())
  name           String
  authorId       Int
  author         User         @relation(fields: [authorId], references: [id])
  users          User[]       @relation("GroupToUserManyToMany")

  @@index([authorId], name: "authorId")
}

Here my table structure:

Bildschirmfoto 2020-10-14 um 12 17 24 PM

Prisma information

this._prisma.group.findMany({
  include: { author: true, users: true },
});

Environment & setup

  • OS: MacOS
  • Database: MySQL
  • Node.js version: 12.18.4
  • Prisma version: 2.9.0

Activity

  1. janpio commented on Oct 14, 2020

    @janpio
    Contributor

    Can you share the SQL of your database (preferably a minimized version) so we can try to reproduce this?

  2. diegozuber commented on Oct 14, 2020

    @diegozuber
    Author

    @janpio of course:

    CREATE TABLE `_GroupToUser` (
      `A` bigint(20) unsigned NOT NULL,
      `B` bigint(20) unsigned NOT NULL,
      UNIQUE KEY `A` (`A`,`B`) USING BTREE,
      KEY `B` (`B`),
      CONSTRAINT `_GroupToUser_ibfk_1` FOREIGN KEY (`A`) REFERENCES `Group` (`id`),
      CONSTRAINT `_GroupToUser_ibfk_2` FOREIGN KEY (`B`) REFERENCES `User` (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
    
    CREATE TABLE `Group` (
      `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
      `name` varchar(50) NOT NULL,
      `authorId` bigint(20) unsigned NOT NULL,
      PRIMARY KEY (`id`),
      UNIQUE KEY `id` (`id`),
      KEY `authorId` (`authorId`),
      CONSTRAINT `Group_ibfk_2` FOREIGN KEY (`authorId`) REFERENCES `User` (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
    
    CREATE TABLE `User` (
      `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
      `firstname` varchar(50) NOT NULL,
      `lastname` varchar(50) NOT NULL,
      `email` varchar(50) DEFAULT NULL,
      PRIMARY KEY (`id`),
      UNIQUE KEY `id` (`id`),
      UNIQUE KEY `email` (`email`) USING BTREE
    ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
    
  3. pantharshit00 commented on Nov 4, 2020

    @pantharshit00
    Contributor

    I can reproduce this. I think link table mapping needs to be fixed.

    Reproduction steps:

    1. Bootstrap a database with the above schema
    2. Introspect the database
    3. Try inserting a user record to the database using the client or prisma studio.
  4. removed their assignment
    on Nov 4, 2020
  5. added
    bug/2-confirmed
    kind/bug
    on Nov 4, 2020
  6. mavilein commented on Nov 4, 2020

    @mavilein
    Contributor

    That is probably a problem the handling of ambiguous relations in introspection. Naming both relations explicitly might help to work around this bug:

    model Group {
      id             Int          @id @default(autoincrement())
      name           String
      authorId       Int
      author         User         @relation("AuthorRelation", fields: [authorId], references: [id])
      users          User[]       @relation("GroupToUser")
    
      @@index([authorId], name: "authorId")
    }
    
  7. self-assigned this
    on Nov 4, 2020
  8. pantharshit00 commented on May 4, 2021

    @pantharshit00
    Contributor

    This is still reproducible in 2.21.2. Adding the candidate label.

  9. added this to the 2.23.0 milestone on May 5, 2021
  10. added
    tech/engines
    tech/engines/introspection engine
    on Mar 23, 2022
  11. larsniet commented on Mar 31, 2022

    @larsniet

    It's still an issue in 3.11.1.

  12. larsniet commented on Mar 31, 2022

    @larsniet

    It's still an issue in 3.11.1.

    Further explanation can be found in this issue. Weird how this bug has been around for so long.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug/2-confirmedkind/bugstatus/needs-actiontech/enginestech/engines/introspection enginetopic: introspection

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions