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 6875559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:20:45+00:00 2026-05-27T04:20:45+00:00

I have a table with this layout: CREATE TABLE Favorites ( FavoriteId uuid NOT

  • 0

I have a table with this layout:

CREATE TABLE Favorites (
  FavoriteId uuid NOT NULL PRIMARY KEY,
  UserId uuid NOT NULL,
  RecipeId uuid NOT NULL,
  MenuId uuid
);

I want to create a unique constraint similar to this:

ALTER TABLE Favorites
ADD CONSTRAINT Favorites_UniqueFavorite UNIQUE(UserId, MenuId, RecipeId);

However, this will allow multiple rows with the same (UserId, RecipeId), if MenuId IS NULL. I want to allow NULL in MenuId to store a favorite that has no associated menu, but I only want at most one of these rows per user/recipe pair.

The ideas I have so far are:

  1. Use some hard-coded UUID (such as all zeros) instead of null.
    However, MenuId has a FK constraint on each user’s menus, so I’d then have to create a special "null" menu for every user which is a hassle.

  2. Check for existence of a null entry using a trigger instead.
    I think this is a hassle and I like avoiding triggers wherever possible. Plus, I don’t trust them to guarantee my data is never in a bad state.

  3. Just forget about it and check for the previous existence of a null entry in the middle-ware or in a insert function, and don’t have this constraint.

I’m using Postgres 9.0. Is there any method I’m overlooking?

  • 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-05-27T04:20:46+00:00Added an answer on May 27, 2026 at 4:20 am

    Postgres 15 or newer

    Postgres 15 adds the clause NULLS NOT DISTINCT. The release notes:

    • Allow unique constraints and indexes to treat NULL values as not distinct (Peter Eisentraut)

      Previously NULL values were always indexed as distinct values, but
      this can now be changed by creating constraints and indexes using
      UNIQUE NULLS NOT DISTINCT.

    With this clause null is treated like just another value, and a UNIQUE constraint does not allow more than one row with the same null value. The task is simple now:

    ALTER TABLE favorites
    ADD CONSTRAINT favo_uni UNIQUE NULLS NOT DISTINCT (user_id, menu_id, recipe_id);
    

    There are examples in the manual chapter "Unique Constraints".
    The clause switches behavior for all keys of the same index. You can’t treat null as equal for one key, but not for another.
    NULLS DISTINCT remains the default (in line with standard SQL) and does not have to be spelled out.

    The same clause works for a UNIQUE index, too:

    CREATE UNIQUE INDEX favo_uni_idx
    ON favorites (user_id, menu_id, recipe_id) NULLS NOT DISTINCT;
    

    Note the position of the new clause after the key fields.

    Postgres 14 or older

    Create two partial indexes:

    CREATE UNIQUE INDEX favo_3col_uni_idx ON favorites (user_id, menu_id, recipe_id)
    WHERE menu_id IS NOT NULL;
    
    CREATE UNIQUE INDEX favo_2col_uni_idx ON favorites (user_id, recipe_id)
    WHERE menu_id IS NULL;
    

    This way, there can only be one combination of (user_id, recipe_id) where menu_id IS NULL, effectively implementing the desired constraint.

    Possible drawbacks:

    • You cannot have a foreign key referencing (user_id, menu_id, recipe_id). (It seems unlikely you’d want a FK reference three columns wide – use the PK column instead!)
    • You cannot base CLUSTER on a partial index.
    • Queries without a matching WHERE condition cannot use the partial index.

    If you need a complete index, you can alternatively drop the WHERE condition from favo_3col_uni_idx and your requirements are still enforced.
    The index, now comprising the whole table, overlaps with the other one and gets bigger. Depending on typical queries and the percentage of null values, this may or may not be useful. In extreme situations it may even help to maintain all three indexes (the two partial ones and a total on top).

    This is a good solution for a single nullable column, maybe for two. But it gets out of hands quickly for more as you need a separate partial index for every combination of nullable columns, so the number grows binomially. For multiple nullable columns, see instead:

    • Why doesn’t my UNIQUE constraint trigger?

    Aside: I advise not to use mixed case identifiers in PostgreSQL.

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

Sidebar

Related Questions

I have this table layout: uuid bigint(20) unsigned primary auto_increment timestamp int(11) unsigned name
I have this table CREATE TABLE [dbo].[friend_blocked_list]( [subdomain] [varchar](50) NOT NULL, [un] [nvarchar](50) NOT
I have this table CREATE TABLE `codes` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
I have this table: CREATE TABLE `test` ( `ID` int(11) NOT NULL auto_increment, `text`
I have this table: CREATE TABLE `point` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `siteid`
I have this code to create table layout. In every row, had one OnclickListener
This is my table layout: -- Table structure for table `areas` CREATE TABLE IF
I have table like this : ID Name_1 Name_2 Name_3 1 Egon Spengler Ives
i have this table in UTF8 (collation is utf8_bin in case it matters). I
I have this table: fourn_category (id , sub) I am using this code to

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.