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
Adding scopes after a model has been defined #3963
Comments
Hmm, I agree that includes should probably throw an error if the argument passed is undefined. I have thought about this before (when re-writing scopes actually) - and there is currently no good solution. Of course you could sort the models in the order that best suits your case - in this specific case making sure that A better solution to this would probably be to have an |
Glad this is truly a bug and not just programmer error! I agree, I think an I'll see if I can help contribute to some documentation for the time being. |
@janmeier do you have any tips about implementing this |
@aarosil This is actually the only code that handles scopes on the model now https://github.com/sequelize/sequelize/blob/master/lib/model.js#L647-L651. So an addScope function should call conformIncludes and add the scope to this.options.scopes |
@bomattin I have found a workaround: module.exports = function(sequelize, DataTypes) {
var group = sequelize.define("group", {
...
...
}, {
classMethods: {
associate: function(models) {
...
group.belongsTo(models.groupType, {
foreignKey:"grouptypeid"
});
}
}
}, {
scopes: {
includeType: function() {
return {
include: [{association: group.associations.groupType}]
};
// or
var groupType = group.associations.groupType.target;
return {
include: [{model: groupType}]
};
}
}
});
console.log(sequelize.models);
return group;
}; However, this won't work with |
@irobert91 Workaround works great. Nice find! Thanks. |
Not sure if this is the right place to ask, but it seemed like the most relevant issue. I've noticed that, regardless of what order I specify to load my models, sometimes I get issues where models are undefined at the time I try to reference them in scopes. However, if I define the scope as a function instead of a JS object, things work fine, presumably because the evaluation of the scope as a function is deferred. Is that correct? Is there a better way or should I stick with this method for now? For reference, this works:
But this doesn't:
|
@markdboyd That's fascinating. I suspect you're correct, it absolutely must be because the function evaluation is deferred. We do have |
Yeah, I prefer keeping all my model information together as well. I guess I'll keep this workaround for now |
I know this has been closed but I have found a problem with this workaround. I have been setting my scopes and functions that return an object, but the documentation says that |
how about this method? |
In the
models/index.js
file generated by Sequelize-CLI, when the files are read and their models are appended, this is done alphabetically:models/index.js
I suspect this makes for an awkward situation like the following: Say I have 3 models,
Group
,GroupType
andUser
.In the
defaultScope
ofUser
, I can includeGroup
with the following:However, if I try to include
GroupType
inGroup
's defaultScope, Sequelize fails silently and ignores the include. Why? Well, I'm not sure really. I added a console.log line right before I returned the object for export:models/group.js
And sure enough, groupType is NOT in the sequelize.models object. When I get Group models from a finder, Sequelize fails silently and returns just the Groups, without their types, as though the include was never written.
The text was updated successfully, but these errors were encountered: