Skip to content

feat(postgres, sqlite): allow override of conflict keys in Model.bulkCreate - #13419

Merged
wbourne0 merged 27 commits into
sequelize:mainfrom
wbourne0:fix-bulkInsert-conflict-keys
Feb 17, 2023
Merged

wbourne0 merged 27 commits into
sequelize:mainfrom
wbourne0:fix-bulkInsert-conflict-keys

Conversation

@wbourne0

@wbourne0 wbourne0 commented Aug 5, 2021 •

Copy link
Copy Markdown
Member

Pull Request check-list

Please make sure to review and check all of these items:

  • Does npm run test or npm run test-DIALECT pass with this change (including linting)?
  • Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Have you added new tests to prevent regressions?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
  • Did you update the typescript typings accordingly (if applicable)?
  • Did you follow the commit message conventions explained in CONTRIBUTING.md?

Description of change

Allows for the override of the upsertKeys value passed to the query generator via options.conflictFields (for Model.bulkCreate).

Note that the different name is intentional - upsertKeys isn't as clear as conflictFields IMO. (its unclear whether an upsertKey is referring to a key that will be updated or a key that should cause the existing row to be updated if it matches)

This is a part of #13270 and it is needed for conflictWhere to work (properly at least) in some cases. Though it is worth noting that there should be better logic for determining defaults, I think this is still handy for special edge cases.

Example use case (using a many:many joiner table)

const Memberships: typeof Model = sequelize.define(
  'my_joiner_table',
  {
    id: {
      primaryKey: true,
      autoIncrement: true,
      type: DataTypes.INTEGER,
      allowNull: false,
    },
    group_id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      allowNull: false,
      // Note that the ideal way to make this relation is to have a unqiue partial index on `deletedAt IS NULL`
      // that way the unique constraint will ignore soft-deleted rows.
      // Once #13420 lands, that will be the ideal way to make this type of relation.
      unique: 'my_constraint',
    },
    user_id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      allowNull: false,
      unique: 'my_constraint',
    },
  },
  { timestamps: true }
);

// In usage
await Memberships.bulkCreate(
  [
    {
      group_id: 5,
      team_id: 3,
    },
    {
      group_id: 5,
      team_id: 4,
    },
  ],
  {
    conflictFields: ['group_id', 'user_id'],
  }
); // `ON CONFLICT ("group_id", "user_id")` instead of `ON CONFLICT ("id", "group_id", "user_id")` 

@wbourne0 wbourne0 added the type: feature DEPRECATED: replace with the "feature" issue type label Aug 5, 2021
@wbourne0
wbourne0 force-pushed the fix-bulkInsert-conflict-keys branch from 366d8ac to 949997d Compare August 5, 2021 20:53
@wbourne0 wbourne0 added the status: wip For issues and PRs. Applied when the PR is not ready yet / when work to close the issue has started. label Aug 5, 2021
@wbourne0 wbourne0 self-assigned this Aug 5, 2021
@wbourne0 wbourne0 removed the status: wip For issues and PRs. Applied when the PR is not ready yet / when work to close the issue has started. label Aug 5, 2021
@wbourne0 wbourne0 added dialect: postgres For issues and PRs. Things that involve PostgreSQL (and do not involve all dialects). dialect: sqlite For issues and PRs. Things that involve SQLite (and do not involve all dialects). and removed status: awaiting maintainer labels Oct 10, 2021
@github-actions github-actions Bot added the stale label Oct 27, 2021
@wbourne0 wbourne0 removed the stale label Oct 30, 2021
@github-actions github-actions Bot added the stale label Nov 7, 2021
@wbourne0 wbourne0 removed the stale label Nov 7, 2021
@ThePletch

ThePletch commented Nov 17, 2021 •

Copy link
Copy Markdown

Wanted to note here that this feature doesn't seem to properly support the underscored: [true/false] model option. If you call init for a model and list fields using camel case, then pass underscored: true in the table options, most other sequelize options will internally translate back to the underlying snake-case field name, but this change passes the fields directly into the query, and thus expects them to already be in snake case.

e.g.

Floob.init(
  {
    someField: {
      type: DataTypes.TEXT,
    },
  },
  {
    sequelize,
    underscored: true,
  }
);

Floob.bulkCreate(
  // ...
  {
    conflictFields: ['someField'],
  }
  // ...
);
// -> SequelizeDatabaseError: column "someField" does not exist

(also, you get a type error trying to list the snake case version of the fields in the new attribute, since they're not properties of the model)

@wbourne0

Copy link
Copy Markdown
Member Author

Wanted to note here that this feature doesn't seem to properly support the underscored: [true/false] model option. If you call init for a model and list fields using camel case, then pass underscored: true in the table options, most other sequelize options will internally translate back to the underlying snake-case field name, but this change passes the fields directly into the query, and thus expects them to already be in snake case.

e.g.

Floob.init(
  {
    someField: {
      type: DataTypes.TEXT,
    },
  },
  {
    sequelize,
    underscored: true,
  }
);

Floob.bulkCreate(
  // ...
  {
    conflictFields: ['someField'],
  }
  // ...
);
// -> SequelizeDatabaseError: column "someField" does not exist

(also, you get a type error trying to list the snake case version of the fields in the new attribute, since they're not properties of the model)

This should be fixed now.

@github-actions github-actions Bot added the stale label Dec 13, 2021
@wbourne0 wbourne0 removed the stale label Jan 5, 2022
@github-actions github-actions Bot added the stale label Jan 20, 2022
@wbourne0 wbourne0 removed the stale label Feb 10, 2022
@ephys
ephys self-requested a review March 11, 2022 10:04
Adds an option to override the `upsertKeys` array passed to the query generator via setting `conflictFields`.
Adds some TS tests for Model.bulkCreate.
@wbourne0
wbourne0 force-pushed the fix-bulkInsert-conflict-keys branch from e851dc0 to 265fd22 Compare March 12, 2022 03:02
ephys
ephys previously requested changes Mar 14, 2022

@ephys ephys left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. It has a failing unit test that tries to access the database

This comment is also relevant #13411 (comment) :)

Comment thread test/unit/model/bulk-create.test.js Resolved
@wbourne0
wbourne0 dismissed ephys’s stale review April 4, 2022 20:55

fixed unit test

@WikiRik

WikiRik commented Nov 14, 2022

Copy link
Copy Markdown
Member

@ephys ping

@WikiRik WikiRik mentioned this pull request Dec 5, 2022
4 of 8 tasks
@Kemperino

Copy link
Copy Markdown

is anything blocking to merge this in? :)

ephys
ephys previously requested changes Dec 8, 2022
Comment thread src/model.d.ts Resolved
Comment thread src/model.d.ts Resolved
Comment thread src/model.js Resolved
@nguyenphuocnhatthanh

nguyenphuocnhatthanh commented Jan 17, 2023 •

Copy link
Copy Markdown

When this PR will be approved ?
@allawesome497 can you continue to resolve these comments ?
This PR's helpful.

@wbourne0

Copy link
Copy Markdown
Member Author

is anything blocking to merge this in? :)

When this PR will be approved ? @allawesome497 can you continue to resolve these comments ? This PR's helpful.

Missed these @nguyenphuocnhatthanh @Phillip-Kemper.

I was a bit busy & stopped checking in on these for a while. Now that #13411 has been merged I'll be prioritizing this more then #13420.

@wbourne0
wbourne0 dismissed ephys’s stale review February 13, 2023 21:59

made requested changes

@wbourne0
wbourne0 requested a review from ephys February 13, 2023 21:59

@ephys ephys left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good :)

@wbourne0
wbourne0 merged commit f6f8ba7 into sequelize:main Feb 17, 2023
@wbourne0
wbourne0 deleted the fix-bulkInsert-conflict-keys branch February 17, 2023 18:49
wbourne0 added a commit that referenced this pull request Mar 14, 2023
Adds the conflictAttributes option to Model.bulkCreate.  Backport of #13419
wbourne0 added a commit that referenced this pull request Mar 22, 2023
Adds the conflictAttributes option to Model.bulkCreate.  Backport of #13419
wbourne0 added a commit that referenced this pull request Mar 22, 2023
Adds the conflictAttributes option to Model.bulkCreate.  Backport of #13419
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dialect: postgres For issues and PRs. Things that involve PostgreSQL (and do not involve all dialects). dialect: sqlite For issues and PRs. Things that involve SQLite (and do not involve all dialects). type: feature DEPRECATED: replace with the "feature" issue type

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants