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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:31:43+00:00 2026-06-13T23:31:43+00:00

We have a table with a unique constraint on it, for feedback left from

  • 0

We have a table with a unique constraint on it, for feedback left from one user, for another, in relation to a sale.

ALTER TABLE feedback
ADD CONSTRAINT unique_user_subject_and_sale
UNIQUE (user_id, subject_id, sale_id)

This ensures we don’t accidentally get duplicated rows of feedback.

Currently we sometimes hard-delete feedback left in error and left the user leave it again. We want to change to soft-delete:

ALTER TABLE feedback
ADD COLUMN deleted_at timestamptz

If deleted_at IS NOT NULL, consider the feedback deleted, though we still have the audit trail in our DB (and will probably show it ghosted out to site admins).

How can we keep our unique constraint when we’re using soft-delete like this? Is it possible without using a more general CHECK() constraint the does an aggregate check (I’ve never tried using check constraint like this).

It’s like I need to append a WHERE clause to the constraint.

  • 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-13T23:31:45+00:00Added an answer on June 13, 2026 at 11:31 pm

    Your unique index, later edited out.

    CREATE UNIQUE INDEX feedback_unique_user_subject_and_sale_null
    ON feedback(user_id, subject_id, sale_id)
    WHERE deleted_at IS NULL
    

    Your unique index has at least two side effects that might cause you some trouble.

    1. In other tables, you can’t set a foreign key constraint that references “feedback”. A foreign key reference requires some combination of columns to be declared as either primary key or unique.
    2. Your unique index allows multiple rows that differ only in the “deleted_at” timestamp. So it’s possible to end up with rows that look like the example below. Whether this is a problem is application-dependent.

    Example

    user_id  subject_id  sale_id  deleted_at
    --
    1        1           1        2012-01-01 08:00:01.33
    1        1           1        2012-01-01 08:00:01.34
    1        1           1        2012-01-01 08:00:01.35
    

    PostgreSQL documents this kind of index as a partial index, should you need to Google it sometime. Other platforms use different terms for it–filtered index is one. You can limit the problems to a certain extent with a pair of partial indexes.

    CREATE UNIQUE INDEX feedback_unique_user_subject_and_sale_null
    ON feedback(user_id, subject_id, sale_id)
    WHERE deleted_at IS NULL
    
    CREATE UNIQUE INDEX feedback_unique_user_subject_and_sale_not_null
    ON feedback(user_id, subject_id, sale_id)
    WHERE deleted_at IS NOT NULL
    

    But I see no reason to go to this much trouble, especially given the potential problems with foreign keys. If your table looks like this

    create table feedback (
      feedback_id integer primary key,
      user_id ...
      subject_id ...
      sale_id ...
      deleted_at ...
      constraint unique_user_subj_sale 
        unique (user_id, subject_id, sale_id)
    );
    

    then all you need is that unique constraint on {user_id, subject_id, sale_id}. You might further consider making all deletes use the “deleted_at” column instead of doing a hard delete.

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

Sidebar

Related Questions

I have a unique constraint on one property of my entity: Table(uniqueConstraints = @UniqueConstraint(name
I have a table with a unique constraint on one or more fields. Given
I'm using Hibernate 4.0.1.Final. I have a table with a unique constraint column …
I have a tasks table with a priority column, which has a unique constraint.
I have a database table that has a Unique Key constraint defined to avoid
I have a table in my schema that has a unique constraint on two
We have a user table, every user has an unique email and username. We
I am using an oracle table and have created a unique constraint over four
I have a table with a unique constraint. What is the right way to
The below code works great for creating Unique Constraints : ALTER TABLE <tablename> ADD

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.