Possibly unhandled Error: A valid target model must be defined for the workspaces belongsToManyrelation
See original GitHub issueHi!
I was testing out bookshelf but I am getting this error and I just don’t know how to fix it. Could you please help me out?
I got the following models:
var Bookshelf = require('bookshelf').DB;
var User = require("./user").model;
exports.model = Bookshelf.Model.extend(
{
tableName: "workspaces",
users: function()
{
return this.belongsToMany(User, "userworkspaces", "WorkspaceId", "UserId");
}
});
and
var Bookshelf = require('bookshelf').DB;
var Workspace = require("./workspace").model;
exports.model = Bookshelf.Model.extend(
{
tableName: "users",
hidden: ['password', 'activationToken', 'forgotToken', 'forgotDate'],
workspaces: function()
{
return this.belongsToMany(Workspace, "userworkspaces", "UserId", "WorkspaceId");
},
virtuals:
{
fullName: function()
{
return this.get('firstName') + ' ' + this.get('lastName');
}
}
});
When I do the following it works just fine and as expected
new Users().fetch({
withRelated: ['workspaces']
}).then(function(collection) {
res.send(collection.toJSON());
});
but when I try:
new Workspaces().fetch({
withRelated: ['users']
}).then(function(collection) {
res.send(collection.toJSON());
});
It gives me the following error:
Possibly unhandled Error: A valid target model must be defined for the workspaces belongsToManyrelation
at ModelCtor.Model.SqlModel.extend._relation (C:\projects\bookshelf_test\src\node_modules\bookshelf\bookshelf.js:50:15)
Issue Analytics
- State:
- Created 10 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
BookshelfJS A valid target model must be defined for the ...
In your case it looks like Application.Adapter is an instance of Bookshelf, so if you add the registry plugin to it (with Application.Adapter....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
For anyone coming from Google (like me), here is the current link to the Wiki for the Registry plugin: https://github.com/tgriesser/bookshelf/wiki/Plugin:-Model-Registry
Never mind. I think I just found it. It had to do with the way I was loading the models. The syntax was correct.