Adrian
08/28/2021, 11:09 AMSELECT s.id, s.name, s.logo_url
FROM supplier AS s
JOIN supplier_user su ON s.id = su."supplierId"
WHERE su."userId" = ${id}
AND su.active = TRUE
I tried to build this with a prisma query but i don’t get this to work. May last try was:
return this.prisma.supplier.findMany({
where: {
supplierUser: {
some: {
active: true,
userId: { equals: id }
}
}
}, select: {
id: true,
name: true,
logoUrl: true,
}
});
can someone pleas help me to understand why and how this joined selects work?Hyo
08/28/2021, 12:50 PMschema.prisma .
If the prisma models are correctly written, I guess your code should look like belo.
prisma.supplier.findMany({
where: {
supplierUser: {
active: true,
userId: { equals: id }
}
}, select: {
id: true,
name: true,
logoUrl: true,
}
});Adrian
08/28/2021, 12:53 PMmodel Supplier {
id String @id @default(uuid())
name String
logoUrl String @map("logo_url")
updatedAt DateTime @updatedAt @map("updated_at")
createdAt DateTime @default(now()) @map("created_at")
supplierUser SupplierUser[]
@@map("supplier")
}
model SupplierUser {
id String @id @default(uuid())
customerNumber String @map("customer_number")
supplier Supplier @relation(fields: [supplierId], references: [id])
user User @relation(fields: [userId], references: [id])
active Boolean @map("active")
supplierId String
userId String
@@map("supplier_user")
@@unique([supplierId, userId], name: "unique_supplier_user")
}Adrian
08/28/2021, 12:54 PMHyo
08/28/2021, 1:00 PMprisma.supplierUser.findMany({
where: {
active: true,
userId,
supplierId,
}, select: {
supplier: {
id: true,
name: true,
logoUrl: true,
}
}
});
I am not sure if I am getting it since I don't have good env of yours 😿 But hope it helpsHyo
08/28/2021, 1:18 PMconst result = await prisma.user.findUnique({
where: { id: Number(id) },
}).posts({
where: {
published: false
}
})Adrian
08/28/2021, 1:29 PMthis.prisma.supplier.findMany({
where: {
supplierUser: {
some: {
AND: [
{ active: true },
{ userId: { equals: id } }
]
}
}
},
select: {
id: true,
name: true,
logoUrl: true
}
});
SELECT "public"."supplier"."id", "public"."supplier"."name", "public"."supplier"."logo_url"
FROM "public"."supplier"
WHERE ("public"."supplier"."id") IN (SELECT "t0"."id"
FROM "public"."supplier" AS "t0"
INNER JOIN "public"."supplier_user" AS "j0" ON ("j0"."supplierId") = ("t0"."id")
WHERE ("j0"."active" = true AND "t0"."id" IS NOT NULL));
What i noticed it, that the engine interprets the userId:{equals:id} is iterpreted as is not null 🤔Adrian
08/28/2021, 1:29 PMAdrian
08/28/2021, 7:02 PM{ userId: `${id}` }
What i don’t understand is, why. the id has the type of string already.Hyo
08/29/2021, 12:04 PMAdrian
08/29/2021, 1:56 PMRyan
08/30/2021, 5:48 AM