I
I
Ivan Palamarchuk2016-11-09 19:58:24
PostgreSQL
Ivan Palamarchuk, 2016-11-09 19:58:24

INSERT without duplicates in PostgreSQL?

Good afternoon, there is a table:
ec2aaaac5811453489168e3719a7b8b9.png
Query example:

UPDATE users_data ud
    SET
        count = (ud.count + 1),
        updated_at = now()
    WHERE
        user_id = :uid AND
        command = :command AND
        created_at::date = :now;

INSERT INTO users_data (user_id, command)
    SELECT :uid, :command
    WHERE NOT EXISTS (
        SELECT 1
        FROM users_data
        WHERE
            user_id = :uid AND
            command = :command AND
            created_at::date = :now
        LIMIT 1
    );

But the following is happening, despite the fact that there is a unique index ..
bbe6c672943a433dbb79ceb4968b51b2.png
Maybe someone will tell you what ..

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2016-11-09
@sim3x

207234 != 20725

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question