Replies: 3 comments
-
Hello @agcty 👋 Considering this model: model Cars {
id Int @id @default(autoincrement())
name String
colors String[]
} I was able to get the cars which contains either of the value through hasSome filter: import { PrismaClient, Prisma } from "@prisma/client";
const prisma = new PrismaClient();
// A `main` function so that you can use async/await
async function main() {
await prisma.cars.create({
data: {
name: "BMW",
colors: ["red", "blue"],
},
});
await prisma.cars.create({
data: {
name: "Aston Martin",
colors: ["yellow", "green"],
},
});
const cars = await prisma.cars.findMany({
where: {
colors: {
hasSome: ["red", "green"],
},
},
});
console.log(cars);
}
main()
.catch((e) => {
throw e;
})
.finally(async () => {
await prisma.$disconnect();
}); Here's the response: |
Beta Was this translation helpful? Give feedback.
0 replies
-
Thanks @nurul3101, maybe I should have clarified that I'm using MySQL which doesn't have scalar attributes as far as I'm aware of :( |
Beta Was this translation helpful? Give feedback.
0 replies
-
Hello. For anyone encountering the same issue. I found a workaround using Json field. const colors = ['gray', 'white'];
const getCars = await prisma.cars.findMany({
where: {
OR: colors.map(color => (
{ colors: { array_contains: [color] } }
))
},
}); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Is it possible to write a query that returns any of the array_contains matches, and not all of them?
E.g I'd like to find all cars that have a color of either gray or white, not gray AND white.
Beta Was this translation helpful? Give feedback.
All reactions