Logan
08/23/2022, 11:51 AMgiveawayId
in the giveawayEntry
table is a foreign key to the id
field in the giveaway
table. I'm not 100% confident in my schema setup - which could be the problem.
const findGiveaway = await client.prisma.giveaway.findFirst({
where: {
messageId: interaction.message.id
}
})
const findUser = await client.prisma.giveawayEntry.findFirst({
where: {
userId: interaction.user.id,
giveawayId: findGiveaway.id,
}
})
model Giveaway {
id Int @id @default(autoincrement())
messageId String @db.VarChar(20)
GiveawayEntry GiveawayEntry?
}
model GiveawayEntry {
id Int @id @default(autoincrement())
userId String @db.VarChar(20)
giveawayId Giveaway @relation(fields: [id], references: [id])
}
The error is appearing on the line giveawayId: findGiveaway.id, in the second query to check if the user has already entered the giveaway.
Type 'number' is not assignable to type '(Without<GiveawayRelationFilter, GiveawayWhereInput> & GiveawayWhereInput) | (Without<GiveawayWhereInput, GiveawayRelationFilter> & GiveawayRelationFilter) | undefined'
Nurul
08/23/2022, 12:07 PMconst findUser = await client.prisma.giveawayEntry.findFirst({
where: {
userId: interaction.user.id,
giveawayId: {
id: findGiveaway.id
},
}
})
Logan
08/23/2022, 12:18 PMgiveaway
table and then delete all rows in the giveawayEntry
table when this happens, how could I set this up?
Is there anything special you have to do with the delete query, if prisma schema? Or does it do it automagically when you have setup a foreign key between two tables?Nurul
08/23/2022, 12:24 PMOnDelete
to Cascade
Your schema could look something like this:
model Giveaway {
id Int @id @default(autoincrement())
messageId String @db.VarChar(20)
GiveawayEntry GiveawayEntry?
}
model GiveawayEntry {
id Int @id @default(autoincrement())
userId String @db.VarChar(20)
giveawayId Giveaway @relation(fields: [id], references: [id], onDelete: Cascade)
}
Logan
08/23/2022, 12:44 PMLogan
08/23/2022, 12:52 PMgiveawayEntry
table, with a link to the corresponding giveawayId
in the giveaway
table, how am I able to properly do this?
I am viewing the link: https://www.prisma.io/docs/guides/general-guides/database-workflows/foreign-keys/postgresql
In the example it uses the include
field, which includes the table which it is linked too. In my case, I need to link the id
of the corresponding row in the giveaway
table, so I tried the following:
const giveawayId = findGiveaway.id // this is the id/foreign key this entry needs to use
await client.prisma.giveawayEntry.create({
data: {
userId: interaction.user.id,
voiceMinutes: 0,
roleId: "1",
createdAt: new Date(),
},
include: {
giveawayId: true
}
})
But get an error saying
await client.prisma.giveawayEntry.create(
Foreign key constraint failed on the field: `GiveawayEntry_id_fkey (index)`
This is probably because it doesn't know what giveawayId
is, but when trying to specify this in the data as seen here:
const giveawayId = findGiveaway.id // this is the id/foreign key this entry needs to use
await client.prisma.giveawayEntry.create({
data: {
userId: interaction.user.id,
voiceMinutes: 0,
roleId: "1",
createdAt: new Date(),
giveawayId: {
id: giveawayId
}
},
})
This results in an error saying id
does not exist.
Also tried just directly putting giveawayId
inside of data too, similar to the rest of the query, but then it says number
has no common properties.
Not sure what is wrong.Nurul
08/23/2022, 1:19 PMLogan
08/23/2022, 1:29 PMgiveawayEntry
table, the entry should have a link to the giveaway
table and the coresponding giveaway which has already been created.
For example:
giveaway table:
id: giveawayId
prize: prize
When a user enters this giveaway (It has to already have been made by someone else for them to enter, and many different people can enter the same giveaway) I will add them to the giveawayEntry
table which will have a link to this giveaway via a foreign key
giveawayEntry table:
id: entryId
giveawayId: giveawayId
userId: userId
the giveawayId
field in the giveawayEntry
table is a link to the id
field in the giveaway
table
Users can enter multiple giveaways at any given time, and there can be multiple giveaways active at any time.
Hopefully this makes a bit more sense? Sorry if I have explained it badly.