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

  • Home
  • SEARCH
  • 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 8743199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:38:02+00:00 2026-06-13T11:38:02+00:00

Using postgres sql, is there a way to set up a condition on foreign

  • 0

Using postgres sql, is there a way to set up a condition on foreign key where it’s limited to to another table like a normal foreign key constraint, but also allows the value of 0 to exist without it being in the other table. For example:

table_a:
  id

table_b:
  id
  foreign_key_on_table_a_id

table_a would have a list of things, and table_b relates to table_a, but has the foreign key constraint. I would also like it to allow for a value of 0 even though there is no id of 0 in table_a.

Is this the right constraint to use? Is there another/better way of doing this without adding the value into table_a?

  • 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-13T11:38:03+00:00Added an answer on June 13, 2026 at 11:38 am

    I’d change foreign_key_on_table_a_id to allow NULL values. Then use an FK as usual and put NULLs in there instead of zero. You can have a NULL in a column that references another table.

    Alternatively, you could write a function that returns true if a value is in the other table and false otherwise and then add a CHECK constraint:

    CHECK (your_column = 0 or the_function(your_column))
    

    You won’t get any of the usual cascade behavior for FKs though and this CHECK is a massive kludge.

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

Sidebar

Related Questions

js and Postgres (using this module) and I would like to view the SQL
I am trying to restore a DB using an SQL script, but things foreign
I'm using postgres, and I have a users table which contains users first and
(I am using PostgreSQL) I have a table which stores transactions to an account.
Since I'm using Postgresql and can't use LINQ to SQL, I wrote my own
While editing some records in my PostgreSQL database using sql in the terminal (in
Is there a way that I might query a database located on Server 2
I have a migration where I create a products table like so class CreateProducts
I need a way to insert images into a PostgreSQL database without using forms.
I've created a sql dump file using pg_dump. This export file contains functions which

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.