Linked Questions
17 questions linked to/from Postgresql: Conditionally unique constraint
1 vote
2 answers
1k views
Composite unique constraint with boolean value [duplicate]
I have a table structured as below, tests --------------------------- schoolId | name | isDeleted I don't want to allow duplicate test name for an school if it is active i.e isDeleted = false I am ...
1 vote
0 answers
2k views
Conditional UNIQUE constraint on table? [duplicate]
I have this table: CREATE TABLE mitg.tbl_gch_customers ( pe character varying(10)NOT NULL, name character varying NOT NULL, city character varying, address character varying NOT NULL, ...
0 votes
1 answer
314 views
Create a unique constraint where one column can be null to prevent duplicate record insertion [duplicate]
In Postgres 13.4, trying to create a unique constraint where a record in a cars table is unique by name and user_id. The purchased_at date starts out as NULL but can change to a value once a car is ...
316 votes
5 answers
257k views
How do I ALTER a PostgreSQL table and make a column unique?
I have a table in PostgreSQL where the schema looks like this: CREATE TABLE "foo_table" ( "id" serial NOT NULL PRIMARY KEY, "permalink" varchar(200) NOT NULL, ...
408 votes
4 answers
279k views
In Postgresql, force unique on combination of two columns
I would like to set up a table in PostgreSQL such that two columns together must be unique. There can be multiple values of either value, so long as there are not two that share both. For instance: ...
8 votes
1 answer
4k views
Soft delete with unique constraint in Django
I have models with this layout: class SafeDeleteModel(models.Model): ..... deleted = models.DateTimeField(editable=False, null=True) ...... class MyModel(SafeDeleteModel): ...
5 votes
1 answer
2k views
In postgresql, how can I make "name" unique only when "deleted" is false?
I have the following table CREATE TABLE "prm_project_service_product_and_services" ( "id" BIGSERIAL NOT NULL, "name" VARCHAR(60) NOT NULL, "note" VARCHAR(256) NOT NULL, "version" BIGINT ...
0 votes
1 answer
4k views
TypeORM using SoftRemove and Unique decorator
Help me please resolve next issue. I use NestJS + TypeORM softRemove/softDelete for delete records. And I want to apply @Unique decorator only for undeleted records. My remove method like: public ...
1 vote
1 answer
2k views
In Postgresql, add unique constraint on 2 columns, not equal to a specific value
Have a table named People, column address_id (int) and is_deleted (boolean), how can I add a unique constraint to allow unique address_id + false, and allow multiple address_id + true. e.g. ...
1 vote
2 answers
1k views
Postgres: best way to implement single favourite flag
I have this table: CREATE TABLE public.data_source__instrument ( instrument_id int4 NOT NULL, data_source_id int4 NOT NULL, CONSTRAINT data_source__instrument__pk PRIMARY KEY (data_source_id, ...
0 votes
1 answer
735 views
Postgres primary key allowing collision
The table should have behavior similar to the primary key(Item_id, Date, Status). However, multiple cancel status is allowed. Item_id |Date |Status -------------------------------- 1 ...
0 votes
2 answers
786 views
Remove duplicates for the same product id only
Table holds the images of the products. Each row points to URL of the product's image. Many products have multiple images. url > Image url product_id > Product's ID Some products have duplicate ...
0 votes
1 answer
485 views
Postgres constraint and foreign key
Is it possible to enforce a constraint and foreign key only when all values are not null? For example in a polymorphic relation one object would have multiple foreign keys, but often only one is used, ...
1 vote
1 answer
508 views
How to add a constraint that's partial and also works with overlapping daterange for postgres?
I am using a postgres with daterange field called valid_period in thing_thing table. The query I use to add the constraint CREATE EXTENSION IF NOT EXISTS btree_gist; ALTER TABLE thing_thing ADD ...
1 vote
3 answers
175 views
PostgreSQL disallow having more than one row satisfying the condition
I have a table orders with the columns: id, user_id and status. The status column is enum with values: pending, in_process, completed, declined. I need to disallow a user to have more than one order ...