0

I have to insert unique value for a row but has to put check on 3 columns collectively ((unique value(col1 && col2 && col3) == true)

I found a solution here it is

query = select exists(select 1 from titles where "col1"='${col1}' AND "col2"='${col2}' AND "col3"='${col3}');

This query would return true or false based on the entries of the table

if(query == true)

execute insert into table values ('col1','col2','col3','col4','col5');

I am inserting multiple records from a json file using promises.

Is there any way to execute this in single sequelize.query().

Erwin Brandstetter
186k28 gold badges463 silver badges636 bronze badges
asked Oct 1, 2018 at 8:46

1 Answer 1

1

Create a UNIQUE index or constraint on (col1, col2, col3) - if you don't have one already.

Then use INSERT ... ON CONFLICT ... DO NOTHING. That's simpler, faster and more reliable, and unlike the route you had in mind, it's also safe against race conditions.

Related:

Be aware of NULL handling:

answered Oct 1, 2018 at 9:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.