/** Books.js */
module.exports = {
attributes: {
bookUnits: {collection:'BookUnits', via: 'book'},
libraries: {
collection: 'Libraries',
via: 'book',
through: 'BookUnits'
}
}
/** Libraries.js */
module.exports = {
attributes: {
bookUnits: {
collection: "BookUnits",
via: "library"
},
books: {
collection: "Books",
via: "library",
through: "BookUnits"
}
}
/** BookUnit.js */
module.exports = {
attributes: {
book : { model: 'Books', required: true },
library : { model: 'Libraries', required: true },
count : { type: 'integer', required: true, defaultsTo: 0 }
}
}
Using this kind of relationship throwing error
PROJECT_PATH\node_modules\waterline-schema\lib\waterline-schema\joinTables.js:496
this.collections[linkedCollection].throughTable = this.collections[linkedCollection].throughTable || {};
^
TypeError: Cannot read property 'throughTable' of undefined
at JoinTables.markCustomJoinTables (PROJECT_PATH\node_modules\waterline-schema\lib\waterline-schema\joinTables.
js:496:89)
at new JoinTables (PROJECT_PATH\node_modules\waterline-schema\lib\waterline-schema\joinTables.js:44:10)
at new module.exports (PROJECT_PATH\node_modules\waterline-schema\lib\waterline-schema.js:33:17)
at Waterline.initialize (PROJECT_PATH\node_modules\waterline\lib\waterline.js:104:17)
at buildORM (PROJECT_PATH\node_modules\sails\lib\hooks\orm\build-orm.js:48:15)
at Array.async.auto.instantiatedCollections (PROJECT_PATH\node_modules\sails\lib\hooks\orm\index.js:218:11)
at listener (PROJECT_PATH\node_modules\sails\node_modules\async\lib\async.js:600:42)
at PROJECT_PATH\node_modules\sails\node_modules\async\lib\async.js:542:17
at _arrayEach (PROJECT_PATH\node_modules\sails\node_modules\async\lib\async.js:85:13)
at Immediate.taskComplete (PROJECT_PATH\node_modules\sails\node_modules\async\lib\async.js:541:13)
at processImmediate [as _immediateCallback] (timers.js:383:17)
The reason why this happend is case sensitive waterline schema builder. It was waiting for bookunits but have gotten BookUnits. And it's wrong for it
@Seafnox The collection, model, via and through keys are meant to be set to model _identities_, which are always lowercased. Have you tried your example with all lower-cased values?
Yes, in lowercased relationships is working, as I write above.
Why information about it not in documentation? It's very useful and nessesary information, as for me.
Thanks for the heads-up @Seafnox -- made a note in the associations docs here, they'll be online next time the docs site is updated!
@sgress454 I'm having a similar issue which I am retrieving the attributes of a model programmatically, but it's messing up my code since the model name is Lessons, but the sails.models.modelAboveLesson.attributes returns lessons (lowercase l), and now I have to write code to convert the first letter to uppercase.
Is it preferred to make all the model names lowercase? In the sails examples, they make them uppercase when they run sails generate api Users (Users is uppercased first letter).
@AskYous Where you need uppercased model names after associations? It wasn't be nessesary. i'm mostly sure.
Camelcase for models and controllers in sails is need for standardizing it
When I have a one-to-many association. My project is complex, and I'm even creating an create page dynamically per table. To do this, I access the attributes (using sails.models.myModel.attributes), and when I find out a model's attribute is a model itself, I get the name that model, and I run queries against it. But when I run queries against that model that was associated, I have to know ahead of time that camel case version.
Most helpful comment
@Seafnox The
collection,model,viaandthroughkeys are meant to be set to model _identities_, which are always lowercased. Have you tried your example with all lower-cased values?