简体   繁体   中英

ActiveRecord::StatementInvalid: Mysql::Error:

I tried to google hardcore to get what creates this problem, but nothing helped me, so I'm trying 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??

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 think somewhere in your code you are populating the "id" attribute with 5 and this is causing the problem.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM