-
Notifications
You must be signed in to change notification settings - Fork 69
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
Remove removed
, adds removedAt
and removedBy
fields to Exercise model
#2660
Changes from 8 commits
68a5a3d
6ebe9e0
bb3af6d
cc4210a
76aae44
25982cd
938c626
b52f45b
c81384b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,7 +70,7 @@ const getExercisesData: GetExercisesQuery = { | |
email: '[email protected]', | ||
discordId: '8321' | ||
}, | ||
removed: false, | ||
removedAt: null, | ||
description: '```\nlet a = 5\na = a + 10\n// what is a?\n```', | ||
answer: '15', | ||
explanation: 'You can reassign variables that were created with "let".', | ||
|
@@ -91,7 +91,7 @@ const getExercisesData: GetExercisesQuery = { | |
email: '[email protected]', | ||
discordId: '8321' | ||
}, | ||
removed: false, | ||
removedAt: null, | ||
description: '```\nlet a = 1\na += 2\n// what is a?\n```', | ||
answer: '3', | ||
explanation: '`a += 2` is a shorter way to write `a = a + 2`', | ||
|
@@ -112,7 +112,7 @@ const getExercisesData: GetExercisesQuery = { | |
email: '[email protected]', | ||
discordId: '8321' | ||
}, | ||
removed: false, | ||
removedAt: null, | ||
description: '```\nlet a = 1\na -= 2\n// what is a?\n```', | ||
answer: '-1', | ||
explanation: null, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,7 +28,7 @@ const GET_EXERCISES = gql` | |
slug | ||
} | ||
} | ||
removed | ||
removedAt | ||
description | ||
answer | ||
explanation | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
Warnings: | ||
|
||
- You are about to drop the column `removed` on the `exercises` table. All the data in the column will be lost. | ||
|
||
*/ | ||
-- AlterTable | ||
ALTER TABLE "exercises" DROP COLUMN "removed", | ||
ADD COLUMN "removedAt" TIMESTAMP(3), | ||
ADD COLUMN "removedById" INTEGER; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "exercises" ADD CONSTRAINT "exercises_removedById_fkey" FOREIGN KEY ("removedById") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -149,7 +149,8 @@ model User { | |
comments Comment[] | ||
exercises Exercise[] | ||
exerciseSubmissions ExerciseSubmission[] | ||
Exercise Exercise[] @relation("flaggedExercises") | ||
FlaggedExercise Exercise[] @relation("flaggedExercises") | ||
RemovedExercise Exercise[] @relation("removedExercises") | ||
modules Module[] | ||
starsMentor Star[] @relation("starMentor") | ||
starsGiven Star[] @relation("starStudent") | ||
|
@@ -187,7 +188,9 @@ model Exercise { | |
answer String | ||
testStr String? | ||
explanation String? | ||
removed Boolean? @default(false) | ||
removedAt DateTime? | ||
removedById Int? | ||
removedBy User? @relation("removedExercises", fields: [removedById], references: [id]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something I discovered during testing the changes with Apollo playground: export const exercises = () => {
return prisma.exercise.findMany({
include: {
author: true,
+ flaggedBy: true,
+ removedBy: true,
module: {
include: { lesson: true }
}
}
})
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, getting the related fields requires a sql JOIN so prisma will not do the extra work unless needed and requested by us. |
||
flagReason String? | ||
flaggedAt DateTime? | ||
flaggedById Int? | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's only possible to set
removedAt
default value to aDateTime
, and in the client, we will use it like the following:If it has a default, value, the above condition will be true, and we'd get an opposite behavior for what we want.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you don't set a default value postgresql will set it to
null
so we don't need to explicitly set the default to null in prisma, postgresql will do it and we'll get the desired behaviour. Being limited to datetime as a default value is due to prisma, prisma doesn't let you set null as the default for an optional value but that is the default behaviour.Just adding
removedAt DateTime?
like you have done is enough.