Skip to content

Sqlite: bulkCreate with updateOnDuplicate fails when the model contains a composite key. #11534

Closed

Description

@jpvanoosten

Related issue: #11432

Issue Description

The bulkCreate fails if the model contains a composite key and doesn't explicitly define a primary key.

The upsertKeys array is emptied when the model contains a composite key since the length of the fields array of the compositeKey object will be of size > 1 (which causes this filter to skip it):

options.upsertKeys = _.chain(model.uniqueKeys).values().filter(c => c.fields.length === 1).map('column').value();

My model looks like this:

"use strict";
module.exports = (sequelize, DataTypes) => {
  const User = sequelize.define(
    "User",
    {
      username: { type: DataTypes.STRING, unique: "compositeKey" },
      discordId: { type: DataTypes.STRING, unique: "compositeKey" }
    },
    {}
  );
  User.associate = function(models) {
    // associations can be defined here
  };
  return User;
};

In this case, the model.uniqueKeys object only contains a single compositeKey object.
Shouldn't the model.uniqueKeys object also contain the automatically generated id primary key for this model?

Changing the model to this works:

"use strict";
module.exports = (sequelize, DataTypes) => {
  const User = sequelize.define(
    "User",
    {
      username: { type: DataTypes.STRING, unique: true },
      discordId: { type: DataTypes.STRING, unique: true }
    },
    {}
  );
  User.associate = function(models) {
    // associations can be defined here
  };
  return User;
};

What are you doing?

Upserting users for testing:

models.sequelize
  .sync()
  .then(async () => {
    if (process.env.UPSERT_TEST_USERS === "true") {
      const numTestUsers = parseInt(process.env.NUM_TEST_USERS || 50);
      const testUsers = new Array(numTestUsers).fill(null).map((user, i) => ({
        id: i,
        username: `Test${i}`,
        discordId: `discordId${i}`
      }));

      await models.User.bulkCreate(testUsers, {
        updateOnDuplicate: ["username", "discordId"]
      });
    }
  })
  .then(() => {
    debug("Database synchronization succeeded.");

    /**
     * Listen on provided port, on all network interfaces.
     */
    server.listen(port);
    server.on("error", onError);
    server.on("listening", onListening);
  })
  .catch(err => {
    debug(`Database synchronization failed: ${err}`);
  });

What do you expect to happen?

I expect the bulkCreate method to succeed and upsert the test users to the database.

What is actually happening?

Doing this with this model results in the following error:

SequelizeDatabaseError: SQLITE_ERROR: near ")": syntax error

The error is referring to the ON CONFLICT() since it is an error to have an empty ON CONFLICT clause. This should be:

ON CONFLICT('username', 'discordId')

Additional context

Add any other context or screenshots about the feature request here.

Environment

  • Sequelize version: 5.19.3
  • Node.js version: v10.16.3
  • Operating System: Windows 10
  • If TypeScript related: TypeScript version: N/A

Issue Template Checklist

How does this problem relate to dialects?

  • I think this problem happens regardless of the dialect.
  • I think this problem happens only for the following dialect(s): Postgres, Sqlite
  • I don't know, I was using PUT-YOUR-DIALECT-HERE, with connector library version XXX and database version XXX

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I don't know how to start, I would need guidance.
  • No, I don't have the time, although I believe I could do it if I had the time...
  • No, I don't have the time and I wouldn't even know how to start.

Activity

  1. added
    dialect: sqlite
    status: understood
    type: bug
    on Oct 23, 2019
  2. papb commented on Oct 23, 2019

    @papb
    Member

    Hello, thanks for the report!

    Yes, I have the time but I don't know how to start, I would need guidance.

    Great! What kind of guidance do you want?

  3. added 2 commits that reference this issue on Oct 25, 2019
    13287e0
    ce8261b
  4. added a commit that references this issue on Jan 8, 2020
    c66afd6
  5. sushantdhiman commented on Mar 16, 2020

    @sushantdhiman
    Contributor

    Fixed with #11984

  6. added 4 commits that reference this issue on Jun 9, 2020
    da97bd1
    482c603
    23b1d0e
    42f64bf
  7. added a commit that references this issue on Jul 8, 2020
    43bbf1d
  8. added a commit that references this issue on Oct 10, 2020
    b6a6f53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    dialect: sqlitestatus: understoodtype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions