Hey, im trying to have some Rest enpoints to create students and classes.
However im trying to write the validation logic so a user cant add a student to a non existing class.
however how would i go about checking (making a query) to the classes table to check if the id exists.
I've tried to import the classController in the student controller but you can't do that.
so im at a dead end?
#"verifying" foreign key in controller?
1 messages · Page 1 of 1 (latest)
That logic belongs to the service, since it's a business rule, not data validation rule.
so i should do the actual checking if the id is a valid one in the service?
Yes. I mean - the best solution would be to create a database level constraint (foreign key) so that the insert fails at the database. You then handle that case in the service and respond accordingly. There's no need to add more convoluted service logic around it.
If you're going to hit the database anyway, do it as few times as possible.
so like this then?
you can create a prisma transaction that will undo all changes if a specific query fails
one issue i think im facing is that when prisma detects a failed foreign key contraint it throws a internal server error
aren’t you simply checking to see if something exists?
correct me if i’m wrong, but you want to check to see if classes table has a certain id correct?
yes so that for example theres a class A and B
and i want to make it so you cant make a student thats in class C (cuz that doesnt exist)
but basically yes
so if that’s the case if you create a transaction using this.prisma.$transaction()
you can add multiple queries that essentially rely on each other to get to the end result
transaction also takes uses an async param that is the prisma client
but you would put your const class = where id is === id
and you can check to see if you get a valid object if not you can handle that error in anyway you want
sorry for the lack of syntax n stuck im on my phone
can you hover over it and give me the error?
nevermind
i typed __C__reateStudentDto
ahh
so it’s not red anymore but you’re still failing the foreign key constraint?
correct?
wdym with red?
the createStudentDto
ow yea thats fixed
but it fails the foreign key constraint yes?
yea
can i see the prisma schema?
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model Student {
id Int @id @default(autoincrement())
firstName String
lastName String
Klas Klas? @relation(fields: [klasId], references: [id])
klasId Int?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model Klas {
id Int @id @default(autoincrement())
name String
students Student[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model Rubric {
id Int @id @default(autoincrement())
name String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model Criterea {
id Int @id @default(autoincrement())
name String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model Indicator {
id Int @id @default(autoincrement())
name String
description String
value Int
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
currently im working on "Klas" and "Student"
okay let me clarify a student can only be enrolled in one class? or multiple?
only in one
is that student tied to another class?
this might be a bit of a stretch but can you add another = in that comparison of klas === null
still the same error :/
you can also try if(!klas)
same error
can you log the klas object?
okay soo as expected it is not found, but that doesn’t explain why you’re ever hitting the last part of the statement
or do it work now since you added await/then
i think whats happening is that the klas object that im making is "Technically" not null cuz its a promise
so thats why its passing the if
and hence trying to insert a promise will fail
ow forgot to say
yea works fine rn
alright cool good luck with your project
