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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:44:56+00:00 2026-05-28T13:44:56+00:00

I have a line_items table with following columns: product_id variant_id variant_id is nullable. Here

  • 0

I have a line_items table with following columns:

product_id
variant_id

variant_id is nullable.

Here is the condition:

  • If variant_id is NULL then product_id should be unique.
  • If variant_id has a value then combination of product_id and variant_id should be unique.

Is that possible in PostgreSQL?

  • 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-28T13:44:56+00:00Added an answer on May 28, 2026 at 1:44 pm

    Create a UNIQUE multicolumn index on (product_id, variant_id):

    CREATE UNIQUE INDEX line_items_prod_var_idx ON line_items (product_id, variant_id);
    

    However, this allows multiple entries of (1, null) for (product_id, variant_id) by default, because null values are not considered to be distinct values.
    To make up for that, additionally create a partial UNIQUE index on product_id:

    CREATE UNIQUE INDEX line_items_prod_var_null_idx ON line_items (product_id)
    WHERE variant_id IS NULL;
    

    This way you can enter (1,2), (1,3) and (1, null), but neither of them a second time. Also speeds up queries with conditions on one or both column.

    Or use the NULLS NOT DISTINCT clause in Postgres 15 or later. See:

    • Create unique constraint with null columns
    • PostgreSQL multi-column unique constraint and NULL values
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a database table that holds the following fields: product_id | design_id |
Say I have a database that contains the following: orders(id, total, address_id) line_items(id, product_id,
I have the following SQL script: DECLARE @temp table ( ID int IDENTITY(1, 1),
I have the following JSF table: <?xml version=1.0 encoding=UTF-8?> <!DOCTYPE html PUBLIC -//W3C//DTD XHTML
I have two tables orders, and line_items with following structure: Orders (id = PK,
I have a following model class Order < ActiveRecord::Base has_many :products, :through => :line_items
My database model has the following Users(table) related to orders(table) related to order line
I have the following tables: Orders, Notes, Permits The following columns are in each
I have a table called tbl_homework. There are 3 columns called _id, hw and
I have the following strings in a text file test Table Name.type Market Drinks.tea

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.