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

findOrCreate should eagerly load included models. #1711

Open
JehandadK opened this issue May 6, 2014 · 18 comments
Open

findOrCreate should eagerly load included models. #1711

JehandadK opened this issue May 6, 2014 · 18 comments
Labels
type: feature For issues and PRs. For new features. Never breaking changes.

Comments

@JehandadK
Copy link

db.User.findOrCreate(
{
name: "kam"
},
{ username: "something", email:"something, age:23 },
{
include: [
{ model: db.Language, as: 'learningLanguage' },
{ model: db.Language, as: 'secondaryLanguage' },
{ model: db.Language, as: 'primaryLanguage' }
]
}).success(function (user, created) {
if (created) res.status(201).json(user);
else res.status(200).json(user);
});

I believe this query should create and retuned included models of Language with it? Am I doing something wrong or this is pending.

@mickhansen
Copy link
Contributor

Not supported at the moment, probably wouldn't be hard to implement.
Would just need to add include: options.include to https://github.com/sequelize/sequelize/blob/master/lib/dao-factory.js#L1026.

PR's always welcome.

@JehandadK
Copy link
Author

I have forked and setup tests, they are running fine. Just a word, for this case the Tests would belong to

DAOFactory -> findOrCreate ->

  • should create a user that does not exists
  • should return a old user that exists
  • should return all associated foregin objects with user.
  • should return all included objects mentioned in include

Do they seem right?

@mickhansen
Copy link
Contributor

https://github.com/sequelize/sequelize/blob/master/test/dao-factory/create.test.js#L33 ad them here.

Your two first cases should already be covered. I'm not sure what case 3 covers but case 4 seems like what you would want to go with for this feature.

@devilankur18
Copy link

+1

1 similar comment
@cuva
Copy link

cuva commented Sep 7, 2014

+1

@mickhansen
Copy link
Contributor

This should actually be supported since we changed the signature of findOrCreate

@aprilmintacpineda
Copy link

aprilmintacpineda commented Feb 12, 2018

@mickhansen can you please give an example of a working code with findOrCreate, mine doesn't work, it creates the record when it does not exists but it does not eagerload the associations specified.

models.Conversation.findOrCreate({
  where: {
    [ Op.or ]: [
      {
        recipient_user_id: request.signedinUser.id,
        sender_user_id: request.body.user_id
      },
      {
        sender_user_id: request.signedinUser.id,
        recipient_user_id: request.body.user_id
      }
    ]
  },
  include: [
    'recipient',
    'sender',
    models.Chat
  ],
  defaults: {
    /* defaults */
  }
})
.spread(conversation => {
  /* will output undefined */
  console.log(conversation.Chats);
  console.log(conversation.sender);
  console.log(conversation.recipient);
  /* will have no Chats, sender or recipient */
  console.log(conversation);
});

Eagerloading associations only works when the record exists.

  • sequelize@^4.31.2

@lHumanizado
Copy link

I'm having this problem too using v5 beta 15

@ekristen
Copy link

ekristen commented Feb 2, 2019

Same

@createthis
Copy link

createthis commented Feb 28, 2019

me too with 4.42.0

@ccemeraldeyes
Copy link

This is still a problem, why is this issue closed?

@papb
Copy link
Member

papb commented Sep 24, 2019

@ccemeraldeyes Can you please provide a SSCCE, many people complained already but no one gave a SSCCE, thanks :)

@createthis
Copy link

@papb I created an SSCCE for you to the best of my ability: https://github.com/createthis/sequelize_1711_sscce

@papb papb reopened this Oct 9, 2019
@papb
Copy link
Member

papb commented Oct 9, 2019

@createthis Thanks! I will investigate later.

@papb papb self-assigned this Oct 9, 2019
@papb papb removed the type: feature For issues and PRs. For new features. Never breaking changes. label Oct 9, 2019
@BOBYZH
Copy link

BOBYZH commented May 14, 2020

Still a problem for me in 2020...

@vbhakta8
Copy link

Did anyone get this working? I see the feature is there but it still doesn't seem to be working?

@github-actions
Copy link
Contributor

This issue has been automatically marked as stale because it has been open for 14 days without activity. It will be closed if no further activity occurs within the next 14 days. 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 16, 2021
@santicalcagno
Copy link

Still happening, not stale.

@WikiRik WikiRik added type: feature For issues and PRs. For new features. Never breaking changes. and removed stale labels Nov 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature For issues and PRs. For new features. Never breaking changes.
Projects
None yet
Development

No branches or pull requests