MySQL “duplicate entry for ’127′ for key 1″ in Rails
Posted 113 days ago by Alex in Code, Technology
After building a lightly used rails application for internal use at the company I work for, I got a notification that there was an error in the application. Someone was trying to add a new record and it was failing (500 ISE). I checked the logs and found the following:
ActiveRecord::RecordNotUnique (Mysql2::Error: Duplicate entry '127' for key 1: INSERT INTO...
It turns out that this problem is triggered when the id column is of type TINYINT which has a maximum signed value of 127. This of course causes the above error to occur when record 128 is entered.
I was able to resolve this by changing the column type to INT using the MySQL client.
ALTER TABLE employees MODIFY id INTEGER NOT NULL AUTO_INCREMENT;