首页 > 解决方案 > is it possible to insert record without id wasted when using diesel

问题描述

I am learning using rust to write a web service, now facing a problem that when I am insert data into PostgreSQL 13 using diesel:

diesel::insert_into(songs::table)
        .values(&new_song)
        .on_conflict_do_nothing()
        .execute(&connection)
        .unwrap();

this way will wast some increment id when insert data conflict. For example the current row id is 3, if with some conflict insert, the next id will become 8. is it possible to insert without id wasted? select conflict data in each insert command? This is the id define of PostgreSQL 13:

ALTER TABLE public.songs 
    ALTER id ADD GENERATED ALWAYS AS IDENTITY;

标签: rust

解决方案


推荐阅读