Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8721665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:15:28+00:00 2026-06-13T07:15:28+00:00

Do I really need to check that the the record in another table really

  • 0

Do I really need to check that the the record in another table really exist, if PostgreSQL will check that foreign key constraint anyway?

Normaly you check if there is existing parent record, before creating child record. But it’s another query to execute. So I think if it would be better to just handle the PostgreSQL error and check if it was about that the parent record doesn’t exist (violates foreign key constraint).

Is it better to utilize database server capabilities, or it was not ment for this purpose?
Do you recommend to do all the integrity checking before creating new record?

CREATE TABLE "public"."categories" (
    "id" serial,
    "name" varchar(100),
    CONSTRAINT "categories__id_pkey" PRIMARY KEY ("id")
);

CREATE TABLE "public"."products" (
    "id" serial,
    "category_id" integer NOT NULL,
    "name" varchar(100),
    CONSTRAINT "products__id_pkey" PRIMARY KEY ("id"),
    CONSTRAINT "products__category_id_fkey" FOREIGN KEY ("category_id")
        REFERENCES "categories" ("id") ON DELETE CASCADE
);

A pseudo-code:

if (query("SELECT * FROM categories WHERE id = 123"))
    query("INSERT INTO products (category_id, name) VALUES (123, 'Something')")
else
    throw Error("Parent record does not exist")

try
    query("INSERT INTO products (category_id, name) VALUES (123, 'Something')")
catch (Exception e)
    if (e.code == '23503')
        throw Error("Parent record does not exist")
    else
        throw Error("Some other error occured")

Error code 23503 in PostgreSQL is:


ERROR: insert or update on table "products" violates foreign key constraint "products__category_id_fkey"

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T07:15:29+00:00Added an answer on June 13, 2026 at 7:15 am

    Is it better to utilize database server capabilities, or it was not ment for this purpose

    It was exactly meant for that purpose.

    Just handle the error, otherwise you are doing too much work.

    If you check for the existense of the related rows, you just duplicate the work as PostgreSQL will also do that.

    If you don’t want to care in which order you insert the rows, you can declare the constraint as deferrable initially deferred. In that case all related rows must exist when you commit your transaction, not when you run each statement. Here is a short example: http://sqlfiddle.com/#!12/33de1/1

    In that case you need handle errors when you commit, not when you run each statement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a table that has some fields that another one has
Really need just some guidance : Topological sort by arcs definition (from my question)
I really need the following in magento. When a customer shows the config product
I really need help with this last part of my program. I need to
I really need to limit any unnecessary network traffic and server trips. Solution: common
I Really need help in this ... I'm building an application where I need
I really need help this time - Joomla 2.5 and latest K2. I'm using
I really need horizontal scrolling in my app, so I overrode Gallery and I'm
I really need help on this one. I am having a simple login form
I really need advice on how to do the following. I have tried several

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.