Hello, Trying to write nested data into my db foll...
# orm-help
c
Hello, Trying to write nested data into my db following Prisma docs, but have been getting this error message (below) , and I don’t know how else to make it work. It is telling me that a sub category that I defined in the schema ‘sub’ is undefined. I was thinking I wouldn’t have to loop through the sub categories array in order to get the values that I wanted - can you shoot me syntax that can get me out of this snag? Error:
Copy code
Invalid `prisma.category.create()` invocation:

{
  data: {
+   name: String,
?   description?: String | null,
?   url?: String | null,
    subCategories: {
      createMany: {
        data: {
          '0': {
+           name: String,
?           description?: String | null,
?           url?: String | null,
?           image?: String | null,
            sub: {
            ~~~
              createMany: {
                data: [
                  {
                    name: undefined,
                    description: undefined,
                    url: undefined,
                    image: undefined
                  }
                ]
              }
            },
?           id?: Int,
?           subId?: Int | null,
?           createdAt?: DateTime,
?           updatedAt?: DateTime
          }
        }
      }
    },
?   icon?: String | null,
?   bgColor?: String | null,
?   createdAt?: DateTime,
?   updatedAt?: DateTime
  }
}

Unknown arg `sub` in data.subCategories.createMany.data.0.sub for type SubcategoryCreateManyCategoryInput. Did you mean `subId`?
Argument name for data.subCategories.createMany.data.0.name is missing.
Argument name for data.name is missing.

Note: Lines with + are required, lines with ? are optional.
Schema:
Copy code
model Category {
      id            Int           @id @default(autoincrement())
      name          String        @db.VarChar(255)
      url           String?       @db.VarChar(255)
      icon          String?       @db.VarChar(255)
      description   String?       @db.VarChar(255)
      bgColor       String?       @db.VarChar(255)
      subCategories Subcategory[]
      createdAt     DateTime      @default(now())
      updatedAt     DateTime      @updatedAt
}

model Subcategory {
      id          Int       @id @default(autoincrement())
      name        String    @db.VarChar(255)
      description String?
      image       String?
      url         String?
      category    Category  @relation(fields: [categoryId], references: [id])
      sub         Sub[]
      categoryId  Int
      subId       Int?
      createdAt   DateTime  @default(now())
      updatedAt   DateTime  @updatedAt
      Program     Program[]
}

model Sub {
      id            Int          @id @default(autoincrement())
      name          String       @db.VarChar(255)
      description   String?
      image         String?
      url           String?
      createdAt     DateTime     @default(now())
      updatedAt     DateTime     @updatedAt
      categoryId    Int
      category      Subcategory? @relation(fields: [subcategoryId], references: [id])
      subcategoryId Int?
}
create file:
Copy code
import { PrismaClient } from '@prisma/client';
import { data } from '../../../categoryData';

const ArtData = async (req, res) => {
  const prisma = new PrismaClient({ log: ['query'] });

  try {
    const categoryData = req.body;

    await categoryData.map(async (category) => {
      const { name, description, url, subCategories } = category;
      const {
        subName = name,
        subDescription = description,
        subUrl = url,
        image,
        sub,
      } = subCategories;

      const categories = await prisma.category.create({
        data: {
          name,
          description,
          url,
          subCategories: {
            createMany: {
              data: [
                {
                  name: subName,
                  description: subDescription,
                  url: subUrl,
                  sub: {
                    createMany: {
                      data: [
                        {
                          //  name: innerName,
                        },
                      ],
                    },
                  },
                  image,
                },
              ],
            },
          },
        },
      });
    });

    res.status(201).json({ 'category: ': categoryData });
  } catch (error) {
    console.error(error);
    res.status(500);
    res.json({ error: 'something went wrong', error });
  } finally {
    await prisma.$disconnect();
  }
};

export default ArtData;
n
Hey Chris 👋 It seems that you are trying to do nested createMany which is not possible at the moment. Here’s how you could do it by using nested create
Copy code
const categories = await prisma.category.create({
    data: {
      name: 'test',
      description: 'test',
      url: '<https://test.com>',
      subCategories: {
        create: {
          name: 'test',
          description: 'test',
          url: '<https://test.com>',
          sub: {
            create: {
              name: 'test',
              categoryId: 1,
              description: 'test',
              url: '<https://test.com>',
            },
          },
        },
      },
    },
  });
👍 1
c
Thank you Nurul, I will give this a shot. Thanks again 🙏