question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Model.create and Model.bulkCreate include option doesn't seem to work

See original GitHub issue

Issue Creation Checklist

  • I understand that my issue will be automatically closed if I don’t fill in the requested information
  • I have read the contribution guidelines

Bug Description

BuildOptions and BulkCreateOptions have include, but it doesn’t seem to work as far as I can tell. The docs don’t list that as an option for Model.bulkCreate but it exists in the BulkCreateOptions type. It does list it as an option for Model.create. The docs for the include parameter say to see the docs for Model#set, but I don’t see anything in the set entry that seems to explain that parameter.

Reproducible Example

Here is the link to the SSCCE for this issue: https://github.com/hexpunk/sequelize-sscce/tree/bulk-create-options

https://github.com/hexpunk/sequelize-sscce/blob/c59ddd8595da13f8860c833aaa3a1e2ba6ddf788/src/sscce-sequelize-6.ts#L30-L91

What do you expect to happen?

I expect the model instance returned from Model.create to preload or prefetch the relationships given in the include parameter of the passed in options object.

What is actually happening?

No relationships are preloaded or prefetched in the returned model instance.

Environment

  • Sequelize version: 6.21.0
  • Node.js version: v14.19.1
  • If TypeScript related: TypeScript version: 4.2.4
  • Database & Version: PostgreSQL 11.10 and Sqlite3 v3.39.1 (shipped with sqlite3@5.0.9)
  • Connector library & Version: pg@8.7.1 and sqlite3@5.0.9

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 will need guidance.
  • No, I don’t have the time, but my company or I are supporting Sequelize through donations on OpenCollective.
  • No, I don’t have the time, and I understand that I will need to wait until someone from the community or maintainers is interested in resolving my issue.

Indicate your interest in the resolution of this issue by adding the 👍 reaction. Comments such as “+1” will be removed.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ephyscommented, Nov 4, 2022

It’s not about the include being written incorrectly, it’s written correctly. The problem is different:

In this code snippet, the include option is not used to select the team after the player has been created. That is not supported. It’s used to create an associated row

// this include does nothing:
// - it's not used to SELECT the team
// - no team has been provided so it won't create one either
const createdPlayer = await Player.create(
  { name: "Player 1", teamId: team.id },
  { include: [{ model: Team, association: "team" }] }
);

// This inserts a player and associates it to an existing team
const createdPlayer = await Player.create(
  { name: "Player 1", teamId: team.id },
);

// This inserts a NEW team then inserts a new player. This is the only purpose of the "include" in Model.create
const createdPlayer = await Player.create(
  { name: "Player 1", team: { name: "My new team" } },
  { include: [{ association: "team" }] }
);

// To select the team you need to load it separately:
const createdPlayer = await Player.create(
  { name: "Player 1", teamId: team.id },
);

const playerTeam = await createdPlayer.getTeam();
0reactions
fzn0xcommented, Nov 4, 2022

Thanks ephys! My bad as short sleeper,

I’d keep the issue short and focus the useful conclusion.

Read more comments on GitHub >

github_iconTop Results From Across the Web

create/bulkCreate doesn't work with Model.build() #12746
Issue Description If you create a models using Model.build() and then pass those models into create or bulkCreate, it tries to insert all ......
Read more >
Bulk Create Sequlize with where condition - Stack Overflow
I want to update multiple rows arrived from array of objects. currently my code working but it seems to slowly. my cyrrent code...
Read more >
django-bulk-update-or-create - PyPI
bulk_update_or_create for Django model managers. ... Add option to use bulk_create for creates: assert model is not multi-table, if enabled
Read more >
How to use 'sequelize.sync()' without difficulties - Medium
That means you have to create a whole migration script like below ... In conclusion, use 'force' option to sync between model schema...
Read more >
Learn Sequelize in 7 Minutes: Part 1 | by Shraddha Paghdar
We are going to use Sequelize with the Express Node.js framework. so ... bulkCreate is very similar to Model.create, by receiving an array ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found