0

I use PostgreSQL and try to add the index below:

CREATE UNIQUE INDEX product_type_serial_pin_unique
ON servicestore.product_unit(serial)
WHERE serial is not null AND product_id = (SELECT id FROM servicestore.product WHERE product_type = 'SERIAL_PIN');

I got the following error:

ERROR:  cannot use subquery in index predicate

how can I solve this problem?

1
  • 6
    So what would you expect to happen if that sub-query returns a different result tomorrow? And what if that sub-query returns more than one product (which seems quite likely) Commented Feb 10, 2021 at 11:02

1 Answer 1

1

The only way you can make that work with a constraint is if you add an attribute to product_unit that copies product_type from product. To make sure they are in sync, add the attribute to a foreign key from product_unit to product. Alternatively, you can use triggers to synchronize the redundant data.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.