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.

Scope applies to find but not create during findOrCreate call

See original GitHub issue

Right now I’ve got a scoped Model and am trying to do a Model#findOrCreate() on a table with two fields: name and category. category is scoped on the model to always be a specific value for the file I’m in, and I’m trying to do bulk find/creates for several different values.

From looking through the code, it looks like the findOne call is getting the scoped values, but the create is not. Values are inserted into the table without the scope values, which means a second findOrCreate will create a duplicate entry

Here’s a quick example:

var Model = require('./myModel');
Model.scope('cats').findOrCreate( { where: { name: 'Cheshire' } } ).then(function () {
    // Will create a second instance in the DB
    Model.scope('cats').findOrCreate( { where: { name: 'Cheshire' } } );
});

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
mimoncommented, Sep 13, 2016

Any updates on this bug?

2reactions
hankehlycommented, May 25, 2017

@callmekatootie

Reloading the instance returned from findOrCreate seems to apply the defaultScope

let [user] = await User.findOrCreate({
    where: {
        email: profile.email
    }
});

user = await user.reload();
Read more comments on GitHub >

github_iconTop Results From Across the Web

Find or Create with Eloquent - Stack Overflow
Put this in any model and use // Modelname::findOrCreate($id); public static ... so the method in the Model will be scopeMethodName and call...
Read more >
Model Querying - Finders - Sequelize
Finder methods are the ones that generate SELECT queries. ... The method findOrCreate will create an entry in the table unless it can...
Read more >
sequelize.Model.findOrCreate JavaScript and Node.js code ...
Find a row that matches the query, or build and save the row if none is found The successful result of the promise...
Read more >
Live agent does not create Contact neither Case
Hi all, I have a big problem, hope someone can help me. I successfully configured Live agent in a salesforce production org.
Read more >
The Comprehensive Sequelize Cheatsheet
Things I did not include in this Cheatsheet (with links to official ... To set up a basic model with only attributes and...
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