Home > Net >  Can't connect a model to anothe model on Prisma
Can't connect a model to anothe model on Prisma

Time:09-02

I'm developing a simple project for food ordering and I'm using Node.js, Express, and Prisma as ORM for dealing with SQlite.

This is my actual schema.prisma:

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

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

model Users {
  id        String                     @id @default(uuid())
  email     String
  name      String
  password  String
  admin     Boolean                    @default(false)
  image     String?
  orders    Orders[]
  favorites UsersHasFavoriteProducts[]
  createdAt DateTime                   @default(now()) @map("created_at")
  updatedAt DateTime                   @updatedAt @map("updated_at")

  @@map("users")
}

model UsersHasFavoriteProducts {
  id        String    @id @default(uuid())
  user      Users?    @relation(fields: [userId], references: [id])
  userId    String?   @map("user_id")
  product   Products? @relation(fields: [productId], references: [id])
  productId String?   @map("product_id")
  createdAt DateTime  @default(now()) @map("created_at")
  updatedAt DateTime  @updatedAt @map("updated_at")

  @@map("users_has_favorite_products")
}

model Orders {
  id         String              @id @default(uuid())
  totalPrice Decimal?            @map("total_price")
  payMethod  String              @default("pix") @map("pay_method")
  status     String              @default("payment pending")
  products   OrdersHasProducts[]
  user       Users?              @relation(fields: [userId], references: [id])
  userId     String?             @map("user_id")
  createdAt  DateTime            @default(now()) @map("created_at")
  updatedAt  DateTime            @updatedAt @map("updated_at")

  @@map("orders")
}

model OrdersHasProducts {
  id        String    @id @default(uuid())
  order     Orders?   @relation(fields: [orderId], references: [id])
  orderId   String?   @map("order_id")
  product   Products? @relation(fields: [productId], references: [id])
  productId String?   @map("product_id")
  quantity  Int
  createdAt DateTime  @default(now()) @map("created_at")
  updatedAt DateTime  @updatedAt @map("updated_at")

  @@map("orders_has_products")
}

model Products {
  id          String                     @id @default(uuid())
  name        String
  description String
  price       Decimal
  category    String
  ingredients ProductsHasIngredients[]
  image       String?
  orders      OrdersHasProducts[]
  favorites   UsersHasFavoriteProducts[]
  createdAt   DateTime                   @default(now()) @map("created_at")
  updatedAt   DateTime                   @updatedAt @map("updated_at")

  @@map("products")
}

model ProductsHasIngredients {
  id           String       @id @default(uuid())
  product      Products?    @relation(fields: [productId], references: [id])
  productId    String?      @map("product_id")
  ingredient   Ingredients? @relation(fields: [ingredientId], references: [id])
  ingredientId String?      @map("ingredient_id")
  createdAt    DateTime     @default(now()) @map("created_at")
  updatedAt    DateTime     @updatedAt @map("updated_at")

  @@map("products_has_ingredients")
}

model Ingredients {
  id        String                   @id @default(uuid())
  name      String
  price     Decimal
  quantity  Int
  image     String?
  products  ProductsHasIngredients[]
  createdAt DateTime                 @default(now()) @map("created_at")
  updatedAt DateTime                 @updatedAt @map("updated_at")

  @@map("ingredients")
}

The Controller will receive this JSON:

{
    "name": "Tomato sandwich",
    "description": "Tomato sandwich",
    "price": "22.00",
    "category": "Main",
    "ingredients": "09ea6c56-a289-474a-ac2a-33ccd7512c6e"
}

And on my repository I have a function for add a product on my database:

async create(name, description, price, category, ingredients) {
    return await prisma.products.create({
      data: {
        name,
        description,
        price,
        category,
        ingredients: {
          connect: { id: ingredients },
        },
      },
    });
  }

But I'm not able to create the relationship with the ingredients table, this error occurs:

The required connected records were not found. Expected 1 records to be connected after connect operation on one-to-many relation 'ProductsToProductsHasIngredients', found 
0.

CodePudding user response:

Basically the Ingredients table do not have the ingredient you wanted to to connect while creating the Product. You can do something like Create or Connect or only create instead of connect.

    ingredients: {
      connectOrCreate: {
        where: {
          id: ingredients,
        },
        create: {
          id: ingredients
          ...otherRequiredFields,
        },
      },
    },
  • Related