K
K
Kt0T02021-05-03 08:57:27
PostgreSQL
Kt0T0, 2021-05-03 08:57:27

PostgreSQL: How to disable duplicate?

How can I prevent a duplicate record if it already exists in the table?

Tried like this:

cur.execute("INSERT INTO likes (postid, like_one, like_two) VALUES (%s, %s, %s) ON CONFLICT (postid)", (postid, 0, 0))


gives an error

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Developer, 2021-05-03
@Kt0T0

There are unique keys, fields and indexes for this.

A
Alexander Filippenko, 2021-05-03
@alexfilus

You can create a unique index for all 3 fields, or an insert trigger.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question