Prisma create: Typescript error in field data - Stack Overflow

admin2025-04-30  0

model Category {
  id                Int      @id @default(autoincrement())
  name              String   @map("category_name") @unique
  items             Item[]
  createdAt         DateTime @default(now()) @map("created_at")
  updatedAt         DateTime @updatedAt @map("updated_at")

  @@map("category")
}
model Item {
  id            Int @id @default(autoincrement()) 
  name          String
  url           String
  price         Int
  priceDiscount Int
  category      Category? @relation(fields: [categoryId], references: [id]) 
  categoryId    Int?
  
  @@map("item")
}
    await prisma.item.create({
      data: {
        id: 1,
        name: initialItems[0].name,
        url: initialItems[0].url,
        price: initialItems[0].price,
        priceDiscount: initialItems[0].priceDiscount,
        category: {
          connect: {
            id: initialItems[0].categoryId
          }
        }
      }
    })

Error screen

Hello. Data base is PostgreSQL. Prisma is 6.1.0. Typescript gives an error in data field, when i've defined id field for Item. If item's Id not presented, everything is ok

Can you explain me, what's happened. If item's Id is not provided connect to exist category works fine, and typescript doesn't show error. But if item's Id is provided, for avoiding typescript error I should provide categoryId. no error

model Category {
  id                Int      @id @default(autoincrement())
  name              String   @map("category_name") @unique
  items             Item[]
  createdAt         DateTime @default(now()) @map("created_at")
  updatedAt         DateTime @updatedAt @map("updated_at")

  @@map("category")
}
model Item {
  id            Int @id @default(autoincrement()) 
  name          String
  url           String
  price         Int
  priceDiscount Int
  category      Category? @relation(fields: [categoryId], references: [id]) 
  categoryId    Int?
  
  @@map("item")
}
    await prisma.item.create({
      data: {
        id: 1,
        name: initialItems[0].name,
        url: initialItems[0].url,
        price: initialItems[0].price,
        priceDiscount: initialItems[0].priceDiscount,
        category: {
          connect: {
            id: initialItems[0].categoryId
          }
        }
      }
    })

Error screen

Hello. Data base is PostgreSQL. Prisma is 6.1.0. Typescript gives an error in data field, when i've defined id field for Item. If item's Id not presented, everything is ok

Can you explain me, what's happened. If item's Id is not provided connect to exist category works fine, and typescript doesn't show error. But if item's Id is provided, for avoiding typescript error I should provide categoryId. no error

Share Improve this question asked Jan 5 at 1:01 vintwpvintwp 1
Add a comment  | 

1 Answer 1

Reset to default 0

It seems like the issue is that you're using connect property and specifying and ID at the same time. There's an open issue for this.

If you want to specify an ID instead of letting prisma (or the database) auto-generate one for you, you'll have to avoid using "connect". Make the connected record first, then use it's ID as as the categoryId.

Prisma is an awesome tool, but it does have lots of little quirks like this!

转载请注明原文地址:http://anycun.com/QandA/1746022749a91473.html