简体   繁体   中英

Having a hard time with Prisma/Graphql One to One and One to many

I have a lot of trouble figuring out the 1 to 1 and 1 to many relation i made this schema then I have my graphql derived from that

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

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

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

model Games {
  id         Int      @id @default(autoincrement())
  createdAt  DateTime @default(now())
  updatedAt  DateTime @updatedAt
  short_hand String   @unique @db.VarChar(255)
  name       String   @unique @db.VarChar(255)
  in_boxes   Box[]
  set        Sets[]
}

model User {
  id         Int      @id @default(autoincrement())
  createdAt  DateTime @default(now())
  updatedAt  DateTime @updatedAt
  username   String   @unique @db.VarChar(255)
  password   String   @db.VarChar(255)
  role       Role     @default(USER)
  first_name String?  @db.VarChar(255)
  last_name  String?  @db.VarChar(255)
  store      String?  @db.VarChar(255)
  boxes      Box[]

}

model Store {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  name      String   @unique @db.VarChar(255)
}

model Box {
  id                Int      @id @default(autoincrement())
  createdAt         DateTime @default(now())
  updatedAt         DateTime @updatedAt
  box_number        String   @db.VarChar(100)
  box_second_number String?   @db.VarChar(100)
  set               String   @db.VarChar(255)
  set_list          Sets     @relation(fields: [set], references: [name])
  gameid            Int?     @default(1)
  game              Games?   @relation(fields: [gameid], references: [id])
  User              User?    @relation(fields: [userId], references: [id])
  userId            Int?
}

model Sets {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  name      String   @unique @db.VarChar(255)
  code      String   @unique @db.VarChar(255)
  children  String[]
  in_boxes  Box[]
  game      String?  @db.VarChar(255)
  gamerel   Games?   @relation(fields: [game], references: [short_hand])
  edition   String?
}

enum Role {
  USER
  ADMIN
  CHRIS
}

Basically a user will have boxes that they own ( The boxes has a Game Type and contains a parent Set which Has it own set code and contain set children (array) The game type it self has only name and shortcode My main issue is that when I try to create a set with the resolver code

the graphql
mutation {
createBox (input:{box_number:"001", secondary_number:"A", game:{name:"yu"}, set:"Crucibile of War Unlimit"}) {
  box_number
  set
  game
  id
}
}

the resolver

   createBox: async (_: any, { input }: any, context: Context) => {
      const find = await context.prisma.games.findFirst({
        where: {name: {contains:input.game[0].name,
        mode:"insensitive"}}
        }
      );
      console.log(find);
      console.log(input.set);
      return await context.prisma.box.create({
        data: {

          box_number: input.box_number,
          box_second_number: input.secondary_number,
          gameid: find?.id,
          set: {connect: {
              name: input.set
            },
          },

            },

 

      });
    },

I get

            "  Foreign key constraint failed on the field: `Box_set_fkey (index)`",
            "    at cb 

I'm really confused on how to make it work

In this case, set directly expects the relation so you don't need connect here. This should work:

const find = await prisma.games.findFirst({
    where: { name: { contains: input.game[0].name } },
  })

  await prisma.box.create({
    data: {
      box_number: input.box_number,
      box_second_number: input.secondary_number,
      gameid: find?.id,
      set: input.set,
    },
  })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM