ON CONFLICT ON CONSTRAINT not detecting unique conflicts in postgresql

648 Views Asked by At

I have this line of code

        db.execute("""INSERT INTO warelo.users (email, username, hash, wg_puid) VALUES(%s, %s, %s, %s)
    ON CONFLICT ON CONSTRAINT users_email_username_user_id_key DO NOTHING
    RETURNING (user_id)""",
    (request.form.get("email"), request.form.get("username"), hash, wg_puid))
    result = db.fetchall()

What it's supposed to do is register a user, and to make sure username and emails are unique, if either the user's username, email or user_id already exist, to do nothing. The code itself runs, but I'm still inserting duplicate emails and usernames. It seems that the ON CONFLICT ON CONSTRAINT is never triggering, even when it should. What's wrong with my code here?

My unique constraints here:

0

There are 0 best solutions below