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

Cyclic scope dependency causes infinite loop #4663

alekbarszczewski opened this Issue Oct 16, 2015 · 2 comments


2 participants

alekbarszczewski commented Oct 16, 2015

I have two models User and Company, User belongs to Company / Company hasMany User.
I am setting default scope for user to include it's company.
I am setting default scope for company to include all company users.

var Sequelize = require('sequelize')

var sequelize = new Sequelize('postgres://localhost/db')
var User = sequelize.define('User')
var Company = sequelize.define('Company')


Company.addScope('defaultScope', {
  include: [{ model: User.scope(null) }]
}, { override: true })

User.addScope('defaultScope', {
  include: [{ model: Company.scope(null) }]
}, { override: true })

User.find({ where: { id: 1 } })


Unhandled rejection RangeError: Maximum call stack size exceeded

I think the problem is because in moment when [{ model: User.scope(null) }] is set, User model does not have overrided defaultScope yet. Easiest solution would be to pass scope option in include object to override included model scope. For example:

Company.addScope('defaultScope', {
  include: [{ model: User, scope: null }] // or scope: 'scope1' or scope: ['scope1', 'scope2']
}, { override: true })

I am using scopes for the first time, so maybe I am doing something wrong?

@janmeier janmeier closed this in 217ba18 Oct 20, 2015


This comment has been minimized.


janmeier commented Oct 20, 2015

An unscoped model was not being marked as .scoped = true, which caused the default scope to be injected instead


This comment has been minimized.


alekbarszczewski commented Oct 20, 2015

@janmeier Thank you! I was trying to find out what is causing this on my own but I always ended up with Maximum call stack size exceeded :).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment