B
B
Bogdan2017-08-31 16:59:37
PostgreSQL
Bogdan, 2017-08-31 16:59:37

Autoincrement in INSERT INTO ... ON CONFLICT?

Hello. With INSERT INTO ... ON CONFLICT , auto-creation is triggered for every record. nextval('dishes_products_norms_id_seq'::regclass) . And already at an insertion there is a gap in id.
For example, the first insert. Records are missing and are inserted into the database.

INSERT INTO dishes_products_norms ( dish_id,product_id,children_category_id,amount ) VALUES ( 980190962,28,6,0.02 ),( 980190962,33,6,0.02 ) ON CONFLICT ( dish_id, product_id, children_category_id ) DO UPDATE SET amount = EXCLUDED.amount RETURNING id ;

id field result
"ids": [
        129,
        130
    ]

The second insert, the first record is updated, the second record is being inserted.
INSERT INTO dishes_products_norms ( dish_id,product_id,children_category_id,amount ) VALUES ( 980190962,28,6,0.03 ),( 980190962,33,6,0.02 ) ON CONFLICT ( dish_id, product_id, children_category_id ) DO UPDATE SET amount = EXCLUDED.amount RETURNING id ;

id field result
"ids": [
        129,
        132
    ]

How to make autoincrement not work?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Melkij, 2017-08-31
@bogdan_uman

No way. Works as intended.
sequence does not guarantee continuity. And the sequence of values ​​too does not guarantee.
Guarantees only uniqueness of values.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question