#"verifying" foreign key in controller?

1 messages · Page 1 of 1 (latest)

outer meteor
#

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?

fickle vale
#

That logic belongs to the service, since it's a business rule, not data validation rule.

outer meteor
#

so i should do the actual checking if the id is a valid one in the service?

fickle vale
#

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.

outer meteor
#

so like this then?

dense cliff
outer meteor
dense cliff
#

aren’t you simply checking to see if something exists?

dense cliff
outer meteor
#

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)

dense cliff
#

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

outer meteor
#

what i tought of is this

#

but for some reason it doesnt wanna take my Dto

dense cliff
#

can you hover over it and give me the error?

outer meteor
#

nevermind
i typed __C__reateStudentDto

dense cliff
#

ahh

outer meteor
#

thats the class
not the param FacePalm

#

yea nah

dense cliff
#

so it’s not red anymore but you’re still failing the foreign key constraint?

#

correct?

outer meteor
#

wdym with red?

dense cliff
#

the createStudentDto

outer meteor
#

ow yea thats fixed

dense cliff
#

but it fails the foreign key constraint yes?

outer meteor
#

yea

dense cliff
#

can i see the prisma schema?

outer meteor
#
// 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"

dense cliff
#

okay let me clarify a student can only be enrolled in one class? or multiple?

outer meteor
#

only in one

dense cliff
#

is that student tied to another class?

outer meteor
#

im sending this this is the current classes

#

so it should fail

dense cliff
#

this might be a bit of a stretch but can you add another = in that comparison of klas === null

outer meteor
#

still the same error :/

dense cliff
#

you can also try if(!klas)

outer meteor
dense cliff
#

can you log the klas object?

outer meteor
#

wait
do you have to await a fetch?
FacePalm

#

because this is a klas

dense cliff
#

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

outer meteor
#

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

dense cliff
#

so it works now

#

right?

outer meteor
#

ow forgot to say
yea works fine rn

dense cliff
#

alright cool good luck with your project