views:

502

answers:

3

I tried to google hardcore to get what creates this problem, but nothing helped me, so i'm triing to write here!

while doing: User.create(:name => "daniel") or User.new(:name => 'daniel').save in the rails console, i get this error

ActiveRecord::StatementInvalid: Mysql::Error: Duplicate entry '5' for key 1: INSERT INTO users (name, salt, privilegi, confirmed_mail, avatar_id, id, password, session_num, email) VALUES('daniel', '23404', NULL, NULL, NULL, 5, NULL, NULL, NULL)

this is my SQL table- id row for "user"

| id | int(11) | NO | PRI | NULL | auto_increment | It is on autoincrement, and i'm sure there is no other entry with id 5!

what is happening??

A: 

If you tried to change the storage engine for this table from/to MyISAM/InnoDB and something went wrong, then indices in this table could have been screwed up. Try backing up this database using mysqldump and restoring it back - it might work: http://www.vbulletin.com/forum/showthread.php?t=141322

Alex Kaushovik
A: 

INSERT INTO users (name, salt, privilegi, confirmed_mail, avatar_id, id, password, session_num, email) VALUES('daniel', '23404', NULL, NULL, NULL, 5, NULL, NULL, NULL)

id is field list of the SQL statement with a value of 5. That does not seem right. Since id is an auto increment field it should not be included in the SQL statement.

Will you post the relevant code that populates the User object then creates it? I thinking some where in your code you are populating the "id" attribute with 5 and this is causing the problem.

dave elkins
A: 

I had the same problem, check out the if the are any indexes created on this table that dont allow duplicates. This solved it for me