最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Prisma nested create throws unknown arg error while prisma example works - Stack Overflow

matteradmin6PV0评论

I have some troubles with prisma nested create. The second nested create does not work as it plains that that arg does not exist. If I write the nested create manually, I can see it with typescript that "audio" field is optional and exists on that model.

Here is the model

model CulturalHeritage {
  id                     Int                      @id @default(autoincrement())
  duration               Int
  categoryId             Int
  category               CulturalHeritageCategory @relation(fields: [categoryId], references: [id])
  published              Boolean                  @default(true)
  mediaId                Int?
  media                  Media?                    @relation(fields: [mediaId], references: [id])
  created                DateTime                 @default(now())
  updated                DateTime?                @updatedAt
  expired                DateTime?
  CulturalHeritageLocale CulturalHeritageLocale[]
  MediaCulturalHeritage  MediaCulturalHeritage[]
  CulturalHeritageExibit CulturalHeritageExibit[]
}

model CulturalHeritageLocale {
  id                 Int              @id @default(autoincrement())
  culturalHeritageId Int
  culturalHeritage   CulturalHeritage @relation(fields: [culturalHeritageId], references: [id])
  description        String           @default("<p></p>")
  title              String
  audio              Media?           @relation(fields: [audioId], references: [id])
  audioText          String?
  audioId            Int?
  localeFlag         LocaleFlag       @relation(fields: [localeFlagId], references: [id])
  localeFlagId       Int
  created            DateTime         @default(now())
  updated            DateTime?        @updatedAt
  expired            DateTime?
}


model Media {
  id                     Int                      @id @default(autoincrement())
  path                   String
  extension              String
  name                   String
  type                   String
  size                   Int
  created                DateTime                 @default(now())
  updated                DateTime?                @updatedAt
  expired                DateTime?
  CulturalHeritage       CulturalHeritage[]
  CulturalHeritageLocale CulturalHeritageLocale[]
  ExibitLocale           ExibitLocale[]
  MediaExibit            MediaExibit[]
  MediaCulturalHeritage  MediaCulturalHeritage[]
}

Function:

//...
return await prisma.culturalHeritage.create({
              include: {
                media: true,
                CulturalHeritageLocale: {
                  include: {
                    audio: true,
                  },
                },
              },
              data: {
                duration: args.data.duration,
                category: { connect: { id: args.data.categoryId } },
                published: args.data.published,
                CulturalHeritageLocale: {
                  // create: args.localeData,
                  create: [
                    {
                      description: '<p>123</p>',
                      title: '123',
                      audioText: '',
                      localeFlagId: 2,
                    },
                    {
                      description: '<p>1</p>',
                      title: '1',
                      audio: { // <--- PRISMA ERROR HERE, TYPESCRIPTS STATES THAT ARG IS OPTIONAL AND EXISTS
                        create: {
                          name: 'file_example_OOG_1MG.ogg',
                          path: '163e6e51-7a6b-4d36-acf6-1328cdd87caf.ogg',
                          extension: 'ogg',
                          type: 'audio/ogg',
                          size: 1089524,
                        },
                      },
                      audioText: 'lll',
                      localeFlagId: 1,
                    },
                  ],
                },
                media: { create: args.data.media.create[0] },
              },
            });

Error:

Unknown arg `audio` in data.CulturalHeritageLocale.create.1.audio for type CulturalHeritageLocaleUncheckedCreateWithoutCulturalHeritageInput. Did you mean `audioId`? Available args:
type CulturalHeritageLocaleUncheckedCreateWithoutCulturalHeritageInput {
  id?: Int
  description?: String
  title: String
  audioText?: String | Null
  audioId?: Int | Null
  localeFlagId: Int
  created?: DateTime
  updated?: DateTime | Null
  expired?: DateTime | Null
}

Prisma example:

const user = await prisma.user.create({
  data: {
    email: '[email protected]',
    name: 'Yvette',
    posts: {
      create: [
        {
          title: 'How to make an omelette',
          categories: {
            create: { // <--- Works fine
              name: 'Easy cooking',
            },
          },
        },
        { title: 'How to eat an omelette' },
      ],
    },
  },
  include: {
    // Include posts
    posts: {
      include: {
        categories: true, // Include post categories
      },
    },
  },
})

Is it possible to write deeply nested create operation? Do I need to use transaction for that? Are there any alternatives?

Thank you!

I have some troubles with prisma nested create. The second nested create does not work as it plains that that arg does not exist. If I write the nested create manually, I can see it with typescript that "audio" field is optional and exists on that model.

Here is the model

model CulturalHeritage {
  id                     Int                      @id @default(autoincrement())
  duration               Int
  categoryId             Int
  category               CulturalHeritageCategory @relation(fields: [categoryId], references: [id])
  published              Boolean                  @default(true)
  mediaId                Int?
  media                  Media?                    @relation(fields: [mediaId], references: [id])
  created                DateTime                 @default(now())
  updated                DateTime?                @updatedAt
  expired                DateTime?
  CulturalHeritageLocale CulturalHeritageLocale[]
  MediaCulturalHeritage  MediaCulturalHeritage[]
  CulturalHeritageExibit CulturalHeritageExibit[]
}

model CulturalHeritageLocale {
  id                 Int              @id @default(autoincrement())
  culturalHeritageId Int
  culturalHeritage   CulturalHeritage @relation(fields: [culturalHeritageId], references: [id])
  description        String           @default("<p></p>")
  title              String
  audio              Media?           @relation(fields: [audioId], references: [id])
  audioText          String?
  audioId            Int?
  localeFlag         LocaleFlag       @relation(fields: [localeFlagId], references: [id])
  localeFlagId       Int
  created            DateTime         @default(now())
  updated            DateTime?        @updatedAt
  expired            DateTime?
}


model Media {
  id                     Int                      @id @default(autoincrement())
  path                   String
  extension              String
  name                   String
  type                   String
  size                   Int
  created                DateTime                 @default(now())
  updated                DateTime?                @updatedAt
  expired                DateTime?
  CulturalHeritage       CulturalHeritage[]
  CulturalHeritageLocale CulturalHeritageLocale[]
  ExibitLocale           ExibitLocale[]
  MediaExibit            MediaExibit[]
  MediaCulturalHeritage  MediaCulturalHeritage[]
}

Function:

//...
return await prisma.culturalHeritage.create({
              include: {
                media: true,
                CulturalHeritageLocale: {
                  include: {
                    audio: true,
                  },
                },
              },
              data: {
                duration: args.data.duration,
                category: { connect: { id: args.data.categoryId } },
                published: args.data.published,
                CulturalHeritageLocale: {
                  // create: args.localeData,
                  create: [
                    {
                      description: '<p>123</p>',
                      title: '123',
                      audioText: '',
                      localeFlagId: 2,
                    },
                    {
                      description: '<p>1</p>',
                      title: '1',
                      audio: { // <--- PRISMA ERROR HERE, TYPESCRIPTS STATES THAT ARG IS OPTIONAL AND EXISTS
                        create: {
                          name: 'file_example_OOG_1MG.ogg',
                          path: '163e6e51-7a6b-4d36-acf6-1328cdd87caf.ogg',
                          extension: 'ogg',
                          type: 'audio/ogg',
                          size: 1089524,
                        },
                      },
                      audioText: 'lll',
                      localeFlagId: 1,
                    },
                  ],
                },
                media: { create: args.data.media.create[0] },
              },
            });

Error:

Unknown arg `audio` in data.CulturalHeritageLocale.create.1.audio for type CulturalHeritageLocaleUncheckedCreateWithoutCulturalHeritageInput. Did you mean `audioId`? Available args:
type CulturalHeritageLocaleUncheckedCreateWithoutCulturalHeritageInput {
  id?: Int
  description?: String
  title: String
  audioText?: String | Null
  audioId?: Int | Null
  localeFlagId: Int
  created?: DateTime
  updated?: DateTime | Null
  expired?: DateTime | Null
}

Prisma example:

const user = await prisma.user.create({
  data: {
    email: '[email protected]',
    name: 'Yvette',
    posts: {
      create: [
        {
          title: 'How to make an omelette',
          categories: {
            create: { // <--- Works fine
              name: 'Easy cooking',
            },
          },
        },
        { title: 'How to eat an omelette' },
      ],
    },
  },
  include: {
    // Include posts
    posts: {
      include: {
        categories: true, // Include post categories
      },
    },
  },
})

Is it possible to write deeply nested create operation? Do I need to use transaction for that? Are there any alternatives?

Thank you!

Share Improve this question asked Oct 15, 2021 at 12:16 SlothOverlordSlothOverlord 2,0471 gold badge9 silver badges22 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

Instead of passing localeFlagId directly, you need to pass it via connect like this:

localeFlag: { connect: { id: 1 } }
Post a comment

comment list (0)

  1. No comments so far