P
Prisma•8mo ago
khris

i want a field to be an array in my mysql schema

I've created my model but in the image field I want it to be an array, so I can insert many images. Is there a solution? generator client { provider = "prisma-client-js" } datasource db { provider = "mysql" url = env("DATABASE_URL") } model Product { id Int @id @default(autoincrement()) title String description String category String price Float imageURL String clientId Int createdAt DateTime @default(now()) updatedAt DateTime @default(now()) client Client @relation(references: [id], fields: [clientId], onDelete: Cascade) @@map("products") } If I write "[]" next to imageURL I get an error: Field "imageURL" in model "Product" can't be a list. The current connector does not support lists of primitive types.Prisma
3 Replies
StoicWanderer
StoicWanderer•8mo ago
@khris it also depends on what database provider xou use,but i think it should be at least an enum
keii
keii•8mo ago
its relational so you want to do a separate image table
model ProductImage {
id Int @id @default(autoincremenet())
productId Int
imageUrl String
...CreatedAt UpdatedAt
}
model ProductImage {
id Int @id @default(autoincremenet())
productId Int
imageUrl String
...CreatedAt UpdatedAt
}
Read up on relational models and relationships like one to many relationships you'll learn alot
RaphaelEtim
RaphaelEtim•8mo ago
Hi @keii 👋 MySQL does not support scalar list. That's the reason for that error. See this section of the documentation You can workaround it by creating a separate Image model and establish a one-to-many relationship between Product and Image. For example:
model Product {
id Int @id @default(autoincrement())
title String
description String
category String
price Float
clientId Int
createdAt DateTime @default(now())
updatedAt DateTime @default(now())
client Client @relation(references: [id], fields: [clientId], onDelete: Cascade)
images Image[]

@@map("products")
}

model Image {
id Int @id @default(autoincrement())
url String
productId Int
product Product @relation(fields: [productId], references: [id])

@@map("images")
}
model Product {
id Int @id @default(autoincrement())
title String
description String
category String
price Float
clientId Int
createdAt DateTime @default(now())
updatedAt DateTime @default(now())
client Client @relation(references: [id], fields: [clientId], onDelete: Cascade)
images Image[]

@@map("products")
}

model Image {
id Int @id @default(autoincrement())
url String
productId Int
product Product @relation(fields: [productId], references: [id])

@@map("images")
}
From the example, a Product can have multiple Image instances associated with it, and each Image is linked to a specific Product via the productId field.
Prisma
Prisma Schema API
API reference documentation for the Prisma Schema Language (PSL).
Want results from more Discord servers?
Add your server