P
Prisma3mo ago
yatahaze

TS Type Issue on Nested Selects

Error, console logs, model and query are below. Query returns data as expected. Thanks.
userData?.cart?.cartItems // Property 'cartItems' does not exist on type '{ id: string; userId: string | null; expiresAt: Date | null; createdAt: Date; updatedAt: Date; }'.ts(2339)
userData?.cart?.cartItems // Property 'cartItems' does not exist on type '{ id: string; userId: string | null; expiresAt: Date | null; createdAt: Date; updatedAt: Date; }'.ts(2339)
{
userData: {
deliveryAddresses: [ [Object] ],
billingAddresses: [ [Object] ],
cart: { id: '5e74b112-a088-44b1-ab7e-e10405fe37ac', cartItems: [Array] }
}
}
{
userData: {
deliveryAddresses: [ [Object] ],
billingAddresses: [ [Object] ],
cart: { id: '5e74b112-a088-44b1-ab7e-e10405fe37ac', cartItems: [Array] }
}
}
model Cart {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
userId String? @unique @map("user_id") @db.Uuid
expiresAt DateTime? @map("expires_at")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")

user User? @relation(fields: [userId], references: [id], onDelete: Cascade)

cartItems CartItem[]

@@index([userId], name: "user_id_idx")
@@map("carts")
}

model CartItem {
cartId String @map("cart_id") @db.Uuid

// ...

cart Cart @relation(fields: [cartId], references: [id], onDelete: Cascade)

// ...
}
model Cart {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
userId String? @unique @map("user_id") @db.Uuid
expiresAt DateTime? @map("expires_at")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")

user User? @relation(fields: [userId], references: [id], onDelete: Cascade)

cartItems CartItem[]

@@index([userId], name: "user_id_idx")
@@map("carts")
}

model CartItem {
cartId String @map("cart_id") @db.Uuid

// ...

cart Cart @relation(fields: [cartId], references: [id], onDelete: Cascade)

// ...
}
const select: Prisma.UserSelect = {
...(deliveryAddressId && {
deliveryAddresses: {
where: {
id: deliveryAddressId,
},
},
}),

billingAddresses: {
where: {
id: billingAddressId,
},
},

cart: {
select: {
id: true,

cartItems: {
select: {
quantity: true,
product: {
select: {
id: true,
websitePrice: true,
},
},
},
},
},
},
};

const userData = await prisma.user.findUnique({
relationLoadStrategy: 'join',

where: {
id: user.id,
},

select,
});
const select: Prisma.UserSelect = {
...(deliveryAddressId && {
deliveryAddresses: {
where: {
id: deliveryAddressId,
},
},
}),

billingAddresses: {
where: {
id: billingAddressId,
},
},

cart: {
select: {
id: true,

cartItems: {
select: {
quantity: true,
product: {
select: {
id: true,
websitePrice: true,
},
},
},
},
},
},
};

const userData = await prisma.user.findUnique({
relationLoadStrategy: 'join',

where: {
id: user.id,
},

select,
});
1 Reply
yatahaze
yatahaze3mo ago
Same thing with include, here is the full cartItem model, had to beat the count limit.
model CartItem {
cartId String @map("cart_id") @db.Uuid
productId String @map("product_id")
quantity Int @default(1)

createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")

cart Cart @relation(fields: [cartId], references: [id], onDelete: Cascade)
product Product @relation(fields: [productId], references: [id], onDelete: Cascade)

@@id([cartId, productId])
@@map("cart_items")
}
model CartItem {
cartId String @map("cart_id") @db.Uuid
productId String @map("product_id")
quantity Int @default(1)

createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")

cart Cart @relation(fields: [cartId], references: [id], onDelete: Cascade)
product Product @relation(fields: [productId], references: [id], onDelete: Cascade)

@@id([cartId, productId])
@@map("cart_items")
}
Want results from more Discord servers?
Add your server