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

Upsert fails when primary key attribute and field names differ #4755

Closed
BigDataSamuli opened this Issue Oct 28, 2015 · 1 comment

Comments

3 participants
@BigDataSamuli

BigDataSamuli commented Oct 28, 2015

I'm getting unique constraint violation errors when using upsert with models that have primary keys with custom DB field names. For example:

var UpsertTestModel = sequelize.define(
    "UpsertTest",
    {
        testKey: {
            type: Sequelize.INTEGER,
            primaryKey: true,
            field: "test_key"
        },
        value: {
            type: Sequelize.INTEGER
        }
    },
    {
        tableName: "upsert_test"
    }
).sync({ force: true });

UpsertTestModel.then(model => {
    return model.upsert({
        testKey: 1,
        value: 1
    }).then(() => {
        return model.upsert({
            testKey: 2,
            value: 1
        });
    }).then(() => {
        return model.upsert({
            testKey: 2,
            value: 2
        });
    });
}).done();

The third upsert will fail with SequelizeUniqueConstraintError. Removing the custom field name definition from the model will make the code work.

With a quick look, it seems like QueryInterface.prototype.upsert is trying to create a where-clause with the primary key values, but it's looking for the primary key attribute names in the values argument, which contains the values by field name instead. Thus, no where clause is created and the SQL UPDATE (in Postgres) fails.

@mickhansen mickhansen added the bug label Oct 28, 2015

janmeier added a commit that referenced this issue Jan 30, 2016

@janmeier

This comment has been minimized.

Member

janmeier commented Jan 30, 2016

Fixed in #5298

@janmeier janmeier closed this Jan 30, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment