1

enter image description here

I have the following table channels in my db with id being primary key and telegram_id being unique. If I try to run this code:

db.Create(&models.Channel{
        TelegramID: 5,
        Title:      "ok",
    })

it won't be inserted into db and the duplicate entry error will be logged in console. However if I change TelegramID to be different (for example "6") and run the code, it will insert with ID being equal to 11, not to 10. enter image description here

How can I make it not to auto-increment ID when the data is not inserted?

  • This is because on error the AUTO_INCREMENT values aren't rolled back, because if any other client has been inserted another id succesfully, you end up with duplicates on your next insert. – Fenistil May 04 '22 at 20:19
  • @Fenistil got it, thank you! Now I understand I don't have to worry about it. – Serial Chiller May 05 '22 at 18:52

0 Answers0