The accepted answer should be declared false, because the "?" is mandatoy see this section.
Answer from naivary on Stack OverflowPrisma 1:1 relation with no optional field on MySQL? - Stack Overflow
Optional relation using a required field makes model unusable
database - Prisma 1-1 optional relation - Stack Overflow
Prisma 1-Way Relationship or Optional List?
The accepted answer should be declared false, because the "?" is mandatoy see this section.
You can choose if the side of the relation with a relation scalar should be optional or mandatory. In the following example, profile and profileId are required. This means that you cannot create a User without connecting or making a Profile
model User {
id Int @id @default(autoincrement())
profile Profile @relation(fields: [profileId], references: [id]) // references `id` of `Profile`
profileId Int @unique // relation scalar field (used in the `@relation` attribute above)
}
model Profile {
id Int @id @default(autoincrement())
user User
}
See this section of the docs for required and optional 1-1 relation.
I'm building a bar database, each bar has a table of drinks on the menu, and each drink has a list of user reviews.
I've uploaded a dataset to the drinks table, that a user can search using autocomplete to select which drink they had at the bar.
Problem: Since I'm using the drinks table independently for the autocomplete, they don't each have a place associated with them, and a new instance will need to be added so that each drink is independent to the bar it is served at. Do I need to create an entirely new table for the drinks associated with a bar, and the ones used for autocomplete?
What is a good way of achieving this?
The solution you found in the docs works for one-to-many relations. One-to-one relations can be filtered like this:
const users = await p.user.findMany({
where: { quiz: { isNot: null } },
});
For those seeking a solution in cases involving one-to-one relations, where the goal is to filter records by a field that is null, you can utilize the following Prisma query:
const users = await p.user.findMany({
where: { quiz: { is: null } },
});
This query effectively retrieves records where the 'quiz' field is null within the 'user' model, making it a valuable tool for filtering data in such scenarios.