Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ER_DUP_ENTRY on update #333

Closed
plan8studios opened this Issue Nov 18, 2012 · 3 comments

Comments

2 participants
@plan8studios
Copy link

plan8studios commented Nov 18, 2012

Trying to do an update on a record and getting weird errors...

var conditions = {
   sid: 'SomeStringOfText'
}

models.Message.find({where: conditions}).success(function(messageRecord){
   if(messageRecord){
   // update the message status
   messageRecord.status = body.SmsStatus;
   // save the record
   messageRecord.save().success(function(){
      ...
   });

That's throwing an ER_DUP_ENTRY "Duplicate entry for '218' for key 'PRIMARY'". The query that's generated looks like...

UPDATE `messages` SET `id`='218', ... WHERE 218

Which seems odd to me. Shouldn't it not set the ID and the "WHERE" should look like "WHERE id = 218" ? My message model looks like:

var Message = db.define('message', {
    id: {
        type: Sequelize.INTEGER,
        autoIncrement: true,
        primaryKey: true
    },
    user_id: Sequelize.INTEGER,
    number_id: Sequelize.INTEGER,
    text: Sequelize.TEXT,
    from: Sequelize.STRING,
    to: Sequelize.STRING,
    status: Sequelize.STRING,
    sid: Sequelize.STRING,
    visible: {
        type: Sequelize.BOOLEAN,
        defaultValue: 1
    },
    created: {
        type: Sequelize.DATE,
        defaultValue: Sequelize.NOW
    }
});
@plan8studios

This comment has been minimized.

Copy link
Author

plan8studios commented Nov 18, 2012

Figured it out. I'm selecting on sid and since it's not a primary key, when I run the update, it acts all weird. defined sid as {type: Sequelize.STRING, primaryKey: true} and that fixed it. It's not true at all, but if that's what fixes it, I'll take it.

@plan8studios

This comment has been minimized.

Copy link
Author

plan8studios commented Dec 2, 2012

I guess this hasn't been resolved. Doing a select on an ID column and I'm still getting this ER_DUP_ENTRY when I call save() on the instance...

models.Setting.find({where: {id: 21}}).success(function(settingRow){
    if(settingRow){
        // this doesn't work
        settingRow.value = 200;
        settingRow.save();

        // neither does this
        settingRow.updateAttributes({
            value: 200
        });
    }
});

Am I retarded or doing something wrong? My Setting model looks like...

var Setting = db.define('setting', {
    id: {
        type: Sequelize.INTEGER,
        autoIncrement: true,
        primaryKey: true
    },
    user_id: Sequelize.INTEGER,
    name: Sequelize.STRING,
    value: Sequelize.STRING,
    created: {
        type: Sequelize.DATE,
        defaultValue: Sequelize.NOW
    },
    modified: {
        type: Sequelize.DATE,
        defaultValue: Sequelize.NOW
    }
});
@janmeier

This comment has been minimized.

Copy link
Member

janmeier commented Aug 18, 2013

I just tested the provided code with the newest master, and cannot replicate the problem. Closing, please reopen if this is still a problem @geoffoliver

@janmeier janmeier closed this Aug 18, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.