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

How do I eager load a model after creating it? #12263

Open
2 of 7 tasks
zoutiyx opened this issue May 17, 2020 · 1 comment
Open
2 of 7 tasks

How do I eager load a model after creating it? #12263

zoutiyx opened this issue May 17, 2020 · 1 comment
Labels
type: docs For issues and PRs. Things related to documentation, such as changes in the manuals / API reference.

Comments

@zoutiyx
Copy link

zoutiyx commented May 17, 2020

Issue Description

I have two models Person and Task. The Person and Task model has multiple associations between them as such;

Task.hasOne(Person, { as: 'striker', foreignKey: 'strikerId' });
Task.hasOne(Person, { as: 'support', foreignKey: 'supportId' });
Task.hasOne(Person, { as: 'muscle', foreignKey: 'muscleId' });
Task.hasOne(Person, { as: 'defender', foreignKey: 'defenderId' });

to create this I'd use const task = await Task.create({/*pairs*/});

How do I eager load task directly after creation?

So instead of receiving;

{
  "data": {
    ...
    "strikerId": "782c9318-36c0-419e-9e4a-f1410003d79f",
    "supportId": "8aefe33c-349f-41e0-9f53-b5654e9901fe"
    ...
  }
}

I get;

 {
  "data": {
    ...
    "striker": {
      "id": "782c9318-36c0-419e-9e4a-f1410003d79f",
      "name": "John Doe",
      "position": "striker"
    },
    "defender": {
      "id": "782c9318-36c0-419e-9e4a-f1410003d79f",
      "name": "John Doe",
      "position": "striker"
    }
    ...
  }
}

I've tried doing const task = await Task.create({/*pairs*/}, {include: {model: Person, as: 'strikerId'}});

for a single record

AND

const task = await Task.create({/*pairs*/}, {include: [{model: Person, as: 'striker'}, {model: Person, as: 'defender'}]});

But they don't work. It just returns the usual object. I can confirm that in the find methods, they work perfectly.

What was unclear/insufficient/not covered in the documentation

The documentation is mostly clearly on usage, but it just doesn't work. Also, it alludes to using set but the documentation isn't clear and gives no example on usage.

If possible: Provide some suggestion on how we can enhance the docs

Give examples on usage of methods

Issue Template Checklist

Is this issue dialect-specific?

  • No. This issue is relevant to Sequelize as a whole.
  • Yes. This issue only applies to the following dialect(s): XXX, YYY, ZZZ
  • I don't know.

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.
@zoutiyx zoutiyx added the type: docs For issues and PRs. Things related to documentation, such as changes in the manuals / API reference. label May 17, 2020
@github-actions
Copy link
Contributor

github-actions bot commented Nov 2, 2021

This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂

@github-actions github-actions bot added the stale label Nov 2, 2021
@WikiRik WikiRik removed the stale label Nov 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: docs For issues and PRs. Things related to documentation, such as changes in the manuals / API reference.
Projects
None yet
Development

No branches or pull requests

2 participants