Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

237
Visualizações
API SQL foreign key reference validation

I'm developing an API on node at the moment and have a model that has a lot of foreign key references (15+) to other models.

I'd like to at least validate that the incoming POST payload contains IDs that exist in the other tables, but I'm not sure where to validate this.

If I simply try to insert it into the database, there'll be a SQL foreign key constraint error. I could capture this and attempt to relay the information to the API response.

Should I be doing each validation individually before I try the insert?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

It's up to you. How often are these likely to fail? Optimistic approach is jam it in and hope for the best. Pessimistic is to systematically check each relationship beforehand.

You can always re-word the error to make it more palatable. It's typically the case you can have a closer look at the error to try and figure out what relationship failed, then come up with a custom error that explains in terms someone will understand.

Normally I do this by having a mapping table from table name to the label used to describe that table. In most applications foreign keys don't just fizzle out randomly, that requires interaction from a user.

Maybe during the process of submitting the form something expired, someone deleted something, or some other event occurred. These happen, but far less often than successful submissions.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda