Hey all. I'm currently having a few difficulties ...
# orm-help
l
Hey all. I'm currently having a few difficulties with setting up a relation between two tables in Postgres Prisma client. The following queries are to find the giveaway which the user is trying to enter, and then trying to check if that user has already entered said giveaway. The
giveawayId
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.
Copy code
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,
    }
  })
Copy code
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.
Copy code
Type 'number' is not assignable to type '(Without<GiveawayRelationFilter, GiveawayWhereInput> & GiveawayWhereInput) | (Without<GiveawayWhereInput, GiveawayRelationFilter> & GiveawayRelationFilter) | undefined'
1
n
Hey Logan 👋 You need to run the findUser query like this:
Copy code
const findUser = await client.prisma.giveawayEntry.findFirst({
    where: {
      userId: interaction.user.id,
      giveawayId: {
        id: findGiveaway.id
      },
    }
  })
1
l
Brilliant, thanks, this has resolved the problem. Another quick question. If I wanted to setup cascading deletes whenever a giveaway has been deleted inside of the
giveaway
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?
n
For that you would need to add Referential Action of
OnDelete
to
Cascade
Your schema could look something like this:
Copy code
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)
}
1
l
Thanks! a bunch 🙂
Also, In relation to creating a row in the
giveawayEntry
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:
Copy code
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
Copy code
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:
Copy code
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.
n
I am trying to understand your use case, do you want to create a Giveaway and GiveawayEntry record both in a single create query?
l
No, not quite. What I'm trying to do is when a user enters a giveaway I want to add their entry into the
giveawayEntry
table, the entry should have a link to the
giveaway
table and the coresponding giveaway which has already been created. For example: giveaway table:
Copy code
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:
Copy code
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.