P
Prisma5mo ago
Virtus

ID needed in delete()?

I'm using Postgres and have a schema for Discord Members. To make it simple with join tables, the primary key (named sid) of the table is an auto-incrementing integer, then another unique index care of the guild ID and user ID combo.
model DiscordMembers {
sid Int @id @default(autoincrement())

id String @db.VarChar(20)
guild_id String @db.VarChar(20)
nick String? @db.VarChar(32)
avatar String? @db.VarChar(34) /// 32 + a_ for animated
joined_at DateTime @db.Timestamptz(6)
premium_since DateTime? @db.Timestamptz(6)
deaf Boolean?
mute Boolean?
flags Int?
pending Boolean?
permissions BigInt?
communication_disabled_until DateTime? @db.Timestamptz(6)
created_at DateTime @default(now()) @db.Timestamptz(6)
updated_at DateTime? @updatedAt @db.Timestamptz(6)

user DiscordUsers @relation(fields: [id], references: [id])

roles DiscordMemberRoles[]

// Unique index combination of guild_id and id
@@unique([guild_id, id], map: "user_id_in_guild")
@@map("DiscordMembers")
}
model DiscordMembers {
sid Int @id @default(autoincrement())

id String @db.VarChar(20)
guild_id String @db.VarChar(20)
nick String? @db.VarChar(32)
avatar String? @db.VarChar(34) /// 32 + a_ for animated
joined_at DateTime @db.Timestamptz(6)
premium_since DateTime? @db.Timestamptz(6)
deaf Boolean?
mute Boolean?
flags Int?
pending Boolean?
permissions BigInt?
communication_disabled_until DateTime? @db.Timestamptz(6)
created_at DateTime @default(now()) @db.Timestamptz(6)
updated_at DateTime? @updatedAt @db.Timestamptz(6)

user DiscordUsers @relation(fields: [id], references: [id])

roles DiscordMemberRoles[]

// Unique index combination of guild_id and id
@@unique([guild_id, id], map: "user_id_in_guild")
@@map("DiscordMembers")
}
When I then want to delete a member, I do not use the sid at all, since I also don't know what it would be without first querying it. So I use the unique guild ID and user ID combination
await this.DiscordMembers.delete({
where: {
guild_id: guildId,
id: userId,
}
});
await this.DiscordMembers.delete({
where: {
guild_id: guildId,
id: userId,
}
});
However TypeScript is now throwing a fit because sid is required. Am I using this wrong or what's going on here?
No description
2 Replies
Virtus
VirtusOP5mo ago
Using deleteMany as a hacky bypass is not going to work either, because I also have an upsert query that will have the exact same problem
await this.DiscordMembers.upsert({
create: {
guild_id: guildId,
id: member.user.id,
nick: member.nick ?? null,
avatar: member.avatar ?? null,
joined_at: member.joined_at ?? null,
premium_since: member.premium_since ?? null,
deaf: member.deaf ?? null,
mute: member.mute ?? null,
flags: member.flags ?? null,
pending: member.pending ?? null,
communication_disabled_until: member.communication_disabled_until ?? null,
// @ts-ignore - We can't really depend on this property ever being populated
permissions: member?.permissions ? BigInt(member.permissions) : null,
},
update: {
nick: member.nick ?? null,
avatar: member.avatar ?? null,
joined_at: member.joined_at ?? null,
premium_since: member.premium_since ?? null,
deaf: member.deaf ?? null,
mute: member.mute ?? null,
flags: member.flags ?? null,
pending: member.pending ?? null,
communication_disabled_until: member.communication_disabled_until ?? null,
// @ts-ignore - We can't really depend on this property ever being populated
permissions: member?.permissions ? BigInt(member.permissions) : null,
},
where: {
guild_id: guildId,
id: member.user.id,
}
});
await this.DiscordMembers.upsert({
create: {
guild_id: guildId,
id: member.user.id,
nick: member.nick ?? null,
avatar: member.avatar ?? null,
joined_at: member.joined_at ?? null,
premium_since: member.premium_since ?? null,
deaf: member.deaf ?? null,
mute: member.mute ?? null,
flags: member.flags ?? null,
pending: member.pending ?? null,
communication_disabled_until: member.communication_disabled_until ?? null,
// @ts-ignore - We can't really depend on this property ever being populated
permissions: member?.permissions ? BigInt(member.permissions) : null,
},
update: {
nick: member.nick ?? null,
avatar: member.avatar ?? null,
joined_at: member.joined_at ?? null,
premium_since: member.premium_since ?? null,
deaf: member.deaf ?? null,
mute: member.mute ?? null,
flags: member.flags ?? null,
pending: member.pending ?? null,
communication_disabled_until: member.communication_disabled_until ?? null,
// @ts-ignore - We can't really depend on this property ever being populated
permissions: member?.permissions ? BigInt(member.permissions) : null,
},
where: {
guild_id: guildId,
id: member.user.id,
}
});
Sophon
Sophon3mo ago
I have this same issue, did you ever solve it? actually--misread, sorry
Want results from more Discord servers?
Add your server