PrismaP
Prisma2y ago
3 replies
b0ngl0rd

Implicit many-to-many problems

// schema
model Dealer {
  id             Int       @id @default(autoincrement())
  name           String
  gamesKnown     Game[] // <-- this field
  startTime      DateTime
  breakStartTime DateTime?
  table          Table?    @relation("dealer")
  goingTo        Table?    @relation("relief")
}

model Game {
  id       Int      @id @default(autoincrement())
  gameType String
  gameName String
  tables   Table[]
  knownBy  Dealer[] // <-- this field
}

and I have this piece of code:
const dealers = () => {
    const dealers: Prisma.DealerCreateInput[] = [];
    for (let i = 0; i < 10; i++) {
      const connect: Prisma.GameWhereUniqueInput[] = f.helpers
        .arrayElements(games, { min: 1, max: 3 })
        .map((game) => ({ id: game.id }));
      dealers.push({
        name: f.person.fullName(),
        startTime: new Date(
          date.setHours(f.number.int({ min: 0, max: 23 }), 0, 0, 0)
        ),
        gamesKnown: {
          connect,
        },
      });
    }
    return dealers;
  };
await prisma.dealer.createMany({ // <-- error thrown at this line
    data: dealers(),
  });

Which throws this prisma error:
Unknown argument `gamesKnown`.
at main (E:\...\my_app\prisma\seed\seed.ts:36:3) {
  clientVersion: '5.17.0'
}

Everything is fully typed out by the TS language server with no warnings or errors. I also double-checked my seed outputs, as well as my migrations:
❯ npx prisma migrate status
Environment variables loaded from .env
Prisma schema loaded from prisma\schema.prisma
Datasource "db": SQLite database "dev.db" at "file:./dev.db"

2 migrations found in prisma/migrations

Database schema is up to date!

I'm just trying to seed my database to develop against, and for some reason this is the only relational query that has truly stumped me as I have nothing to go on at this point after combing through the docs and double-checking my work. I'm looking for any hint to get past this, thanks in advance for your time.
Solution
Turns out, for whatever reason the input to createMany doesn't include relational fields? I'm too busy to look into it further, but here is the updated code which succeeded for anyone that might be wondering:
const dealers = () => {
    const dealers: Prisma.DealerCreateInput[] = [];

    for (let i = 0; i < 10; i++) {
      const connect: Prisma.GameWhereUniqueInput[] = f.helpers
        .arrayElements(games, { min: 1, max: 3 })
        .map((game) => ({ id: game.id }));
      dealers.push({
        name: f.person.fullName(),
        startTime: new Date(
          date.setHours(f.number.int({ min: 0, max: 23 }), 0, 0, 0)
        ),
        gamesKnown: {
          connect,
        },
      });
    }
    return dealers;
  };

  const data = dealers();
  await prisma.$transaction(
    data.map((dealer) => prisma.dealer.create({ data: dealer })) // pass an array of queries, instead of one many query.
  );
Was this page helpful?