Skip to content

Update: Handle database relations in case of deleting user #289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions api/functions/graphql/nexus-typegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ export interface NexusGenObjects {
projects: NexusGenRootTypes['ProjectInTournament'][]; // [ProjectInTournament!]!
}
PostComment: { // root type
author: NexusGenRootTypes['User']; // User!
author?: NexusGenRootTypes['User'] | null; // User
body: string; // String!
created_at: NexusGenScalars['Date']; // Date!
id: number; // Int!
Expand Down Expand Up @@ -653,7 +653,7 @@ export interface NexusGenFieldTypes {
Bounty: { // field return type
applicants_count: number; // Int!
applications: NexusGenRootTypes['BountyApplication'][]; // [BountyApplication!]!
author: NexusGenRootTypes['User']; // User!
author: NexusGenRootTypes['User'] | null; // User
body: string; // String!
cover_image: string | null; // String
createdAt: NexusGenScalars['Date']; // Date!
Expand Down Expand Up @@ -784,7 +784,7 @@ export interface NexusGenFieldTypes {
projects: NexusGenRootTypes['ProjectInTournament'][]; // [ProjectInTournament!]!
}
PostComment: { // field return type
author: NexusGenRootTypes['User']; // User!
author: NexusGenRootTypes['User'] | null; // User
body: string; // String!
created_at: NexusGenScalars['Date']; // Date!
id: number; // Int!
Expand Down Expand Up @@ -878,7 +878,7 @@ export interface NexusGenFieldTypes {
usersByNostrKeys: NexusGenRootTypes['NostrKeyWithUser'][]; // [NostrKeyWithUser!]!
}
Question: { // field return type
author: NexusGenRootTypes['User']; // User!
author: NexusGenRootTypes['User'] | null; // User
body: string; // String!
createdAt: NexusGenScalars['Date']; // Date!
excerpt: string; // String!
Expand All @@ -892,7 +892,7 @@ export interface NexusGenFieldTypes {
votes_count: number; // Int!
}
Story: { // field return type
author: NexusGenRootTypes['User']; // User!
author: NexusGenRootTypes['User'] | null; // User
body: string; // String!
comments: NexusGenRootTypes['PostComment'][]; // [PostComment!]!
comments_count: number; // Int!
Expand Down
8 changes: 4 additions & 4 deletions api/functions/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ interface BaseUser {
type Bounty implements PostBase {
applicants_count: Int!
applications: [BountyApplication!]!
author: User!
author: User
body: String!
cover_image: String
createdAt: Date!
Expand Down Expand Up @@ -319,7 +319,7 @@ interface PostBase {
}

type PostComment {
author: User!
author: User
body: String!
created_at: Date!
id: Int!
Expand Down Expand Up @@ -451,7 +451,7 @@ type Query {
}

type Question implements PostBase {
author: User!
author: User
body: String!
createdAt: Date!
excerpt: String!
Expand Down Expand Up @@ -479,7 +479,7 @@ enum RoleLevelEnum {
}

type Story implements PostBase {
author: User!
author: User
body: String!
comments: [PostComment!]!
comments_count: Int!
Expand Down
8 changes: 4 additions & 4 deletions api/functions/graphql/types/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ const Story = objectType({
return post._count.comments;
},
});
t.nonNull.field("author", {
t.field("author", {
type: "User",
resolve: (parent) => {
return (
Expand Down Expand Up @@ -251,7 +251,7 @@ const Bounty = objectType({
t.nonNull.list.nonNull.field("applications", {
type: "BountyApplication",
});
t.nonNull.field("author", {
t.field("author", {
type: "User",
resolve: (parent) => {
return prisma.bounty.findUnique({ where: { id: parent.id } }).user();
Expand Down Expand Up @@ -287,7 +287,7 @@ const Question = objectType({
// }
// });

t.nonNull.field("author", {
t.field("author", {
type: "User",
resolve: (parent) => {
return prisma.question.findUnique({ where: { id: parent.id } }).user();
Expand All @@ -302,7 +302,7 @@ const PostComment = objectType({
t.nonNull.int("id");
t.nonNull.date("created_at");
t.nonNull.string("body");
t.nonNull.field("author", {
t.field("author", {
type: "User",
});
t.int("parentId");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
-- DropForeignKey
ALTER TABLE "FoundersClubMember" DROP CONSTRAINT "FoundersClubMember_user_id_fkey";

-- DropForeignKey
ALTER TABLE "NostrBadgeRequest" DROP CONSTRAINT "NostrBadgeRequest_userId_fkey";

-- DropForeignKey
ALTER TABLE "ProjectMember" DROP CONSTRAINT "ProjectMember_userId_fkey";

-- DropForeignKey
ALTER TABLE "TournamentJudgingRoundJudge" DROP CONSTRAINT "TournamentJudgingRoundJudge_judge_id_fkey";

-- DropForeignKey
ALTER TABLE "TournamentJudgingRoundJudgeScore" DROP CONSTRAINT "TournamentJudgingRoundJudgeScore_judge_id_fkey";

-- DropForeignKey
ALTER TABLE "TournamentOrganizer" DROP CONSTRAINT "TournamentOrganizer_user_id_fkey";

-- DropForeignKey
ALTER TABLE "TournamentParticipant" DROP CONSTRAINT "TournamentParticipant_user_id_fkey";

-- DropForeignKey
ALTER TABLE "UserAction" DROP CONSTRAINT "UserAction_user_id_fkey";

-- DropForeignKey
ALTER TABLE "UserBadge" DROP CONSTRAINT "UserBadge_userId_fkey";

-- DropForeignKey
ALTER TABLE "UserBadgeProgress" DROP CONSTRAINT "UserBadgeProgress_userId_fkey";

-- DropForeignKey
ALTER TABLE "UserEmail" DROP CONSTRAINT "UserEmail_user_id_fkey";

-- DropForeignKey
ALTER TABLE "UserKey" DROP CONSTRAINT "UserKey_user_id_fkey";

-- DropForeignKey
ALTER TABLE "UserNostrKey" DROP CONSTRAINT "UserNostrKey_user_id_fkey";

-- DropForeignKey
ALTER TABLE "UsersOnWorkRoles" DROP CONSTRAINT "UsersOnWorkRoles_userId_fkey";

-- AddForeignKey
ALTER TABLE "UserKey" ADD CONSTRAINT "UserKey_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "UserNostrKey" ADD CONSTRAINT "UserNostrKey_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "UserEmail" ADD CONSTRAINT "UserEmail_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "UsersOnWorkRoles" ADD CONSTRAINT "UsersOnWorkRoles_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ProjectMember" ADD CONSTRAINT "ProjectMember_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "TournamentParticipant" ADD CONSTRAINT "TournamentParticipant_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "FoundersClubMember" ADD CONSTRAINT "FoundersClubMember_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "UserAction" ADD CONSTRAINT "UserAction_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "UserBadgeProgress" ADD CONSTRAINT "UserBadgeProgress_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "UserBadge" ADD CONSTRAINT "UserBadge_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "NostrBadgeRequest" ADD CONSTRAINT "NostrBadgeRequest_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "TournamentJudgingRoundJudge" ADD CONSTRAINT "TournamentJudgingRoundJudge_judge_id_fkey" FOREIGN KEY ("judge_id") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "TournamentJudgingRoundJudgeScore" ADD CONSTRAINT "TournamentJudgingRoundJudgeScore_judge_id_fkey" FOREIGN KEY ("judge_id") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "TournamentOrganizer" ADD CONSTRAINT "TournamentOrganizer_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
38 changes: 19 additions & 19 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ model Vote {
paid Boolean @default(false)

user_id Int?
user User? @relation(fields: [user_id], references: [id])
user User? @relation(fields: [user_id], references: [id], onDelete: SetNull)
}

// -----------------
Expand Down Expand Up @@ -109,7 +109,7 @@ model UserKey {
name String @default("My new wallet key")
createdAt DateTime @default(now())

user User? @relation(fields: [user_id], references: [id])
user User? @relation(fields: [user_id], references: [id], onDelete: Cascade)
user_id Int?
}

Expand All @@ -120,7 +120,7 @@ model UserNostrKey {
is_primary Boolean @default(false)
is_default_generated_key Boolean @default(false)

user User? @relation(fields: [user_id], references: [id])
user User? @relation(fields: [user_id], references: [id], onDelete: Cascade)
user_id Int?
}

Expand All @@ -129,7 +129,7 @@ model UserEmail {
email String @unique
createdAt DateTime @default(now())

user User? @relation(fields: [user_id], references: [id])
user User? @relation(fields: [user_id], references: [id], onDelete: Cascade)
user_id Int?
}

Expand All @@ -141,7 +141,7 @@ model Otp {
}

model UsersOnWorkRoles {
user User @relation(fields: [userId], references: [id])
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId Int
role WorkRole @relation(fields: [roleId], references: [id])
roleId Int
Expand Down Expand Up @@ -243,7 +243,7 @@ model ProjectRecruitRoles {
model ProjectMember {
project Project @relation(fields: [projectId], references: [id])
projectId Int
user User @relation(fields: [userId], references: [id])
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId Int

role String // Admin | Maker | (new_roles_later)
Expand Down Expand Up @@ -284,7 +284,7 @@ model Story {

tags Tag[]

user User? @relation(fields: [user_id], references: [id])
user User? @relation(fields: [user_id], references: [id], onDelete: SetNull)
user_id Int?

comments PostComment[] @relation("StoryComment")
Expand All @@ -305,7 +305,7 @@ model Question {

tags Tag[]

user User? @relation(fields: [user_id], references: [id])
user User? @relation(fields: [user_id], references: [id], onDelete: SetNull)
user_id Int?

comments PostComment[] @relation("QuestionComment")
Expand All @@ -322,7 +322,7 @@ model PostComment {
parent_comment_id Int?
parent_comment PostComment? @relation("PostComment_Replies", fields: [parent_comment_id], references: [id])

user User? @relation(fields: [user_id], references: [id])
user User? @relation(fields: [user_id], references: [id], onDelete: SetNull)
user_id Int?

story Story? @relation("StoryComment", fields: [story_id], references: [id])
Expand Down Expand Up @@ -363,7 +363,7 @@ model Donation {
preimage String?
paid Boolean @default(false)

donor User? @relation(fields: [donor_id], references: [id])
donor User? @relation(fields: [donor_id], references: [id], onDelete: SetNull)
donor_id Int?
}

Expand Down Expand Up @@ -492,7 +492,7 @@ model TournamentEvent {
model TournamentParticipant {
tournament Tournament @relation(fields: [tournament_id], references: [id])
tournament_id Int
user User @relation(fields: [user_id], references: [id])
user User @relation(fields: [user_id], references: [id], onDelete: Cascade)
user_id Int
createdAt DateTime @default(now())

Expand Down Expand Up @@ -536,7 +536,7 @@ model FoundersClubInvitation {
model FoundersClubMember {
id Int @id @default(autoincrement())
email String
user User @relation(fields: [user_id], references: [id])
user User @relation(fields: [user_id], references: [id], onDelete: Cascade)
user_id Int @unique // relation scalar field (used in the `@relation` attribute above)
}

Expand All @@ -561,7 +561,7 @@ model UserAction {

failReason String?

user User @relation(fields: [user_id], references: [id])
user User @relation(fields: [user_id], references: [id], onDelete: Cascade)
user_id Int

createdAt DateTime @default(now())
Expand Down Expand Up @@ -596,7 +596,7 @@ model UserBadgeProgress {
badge Badge @relation(fields: [badgeId], references: [id])
badgeId Int

user User @relation(fields: [userId], references: [id])
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId Int

progress Int @default(0)
Expand All @@ -608,7 +608,7 @@ model UserBadge {
badge Badge @relation(fields: [badgeId], references: [id])
badgeId Int

user User @relation(fields: [userId], references: [id])
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId Int

badgeAwardNostrEventId String?
Expand All @@ -624,7 +624,7 @@ model NostrBadgeRequest {
badge Badge @relation(fields: [badgeId], references: [id])
badgeId Int

user User @relation(fields: [userId], references: [id])
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId Int

publicKeyToAward String
Expand Down Expand Up @@ -668,7 +668,7 @@ model TournamentJudgingRoundJudge {
round TournamentJudgingRound @relation(fields: [round_id], references: [id])

judge_id Int
judge User @relation(fields: [judge_id], references: [id])
judge User @relation(fields: [judge_id], references: [id], onDelete: Cascade)

@@unique([round_id, judge_id])
}
Expand All @@ -680,7 +680,7 @@ model TournamentJudgingRoundJudgeScore {
round TournamentJudgingRound @relation(fields: [round_id], references: [id])

judge_id Int
judge User @relation(fields: [judge_id], references: [id])
judge User @relation(fields: [judge_id], references: [id], onDelete: Cascade)

project_id Int
project Project @relation(fields: [project_id], references: [id])
Expand All @@ -697,7 +697,7 @@ model TournamentOrganizer {
tournament Tournament @relation(fields: [tournament_id], references: [id])

user_id Int
user User @relation(fields: [user_id], references: [id])
user User @relation(fields: [user_id], references: [id], onDelete: Cascade)

@@id([tournament_id, user_id])
}
Loading