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

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

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

Comments

@BigDataSamuli
Copy link

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.

janmeier added a commit that referenced this issue Jan 30, 2016
@janmeier
Copy link
Member

Fixed in #5298

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

No branches or pull requests

3 participants