Waterline, error trying to create a one-to-many association

I have these models:

// Material.js module.exports = { attributes: { name: { type: 'string', required: true }, source_info: { type: 'string', required: true }, category: { model: 'category_mat' } } }; 

and

 // Category_Mat.js module.exports = { attributes: { name: { type: 'string', required: true }, material:{ collection: 'material', via: 'category' } }, }; 

but when I run the application, I get this error:

 /usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/waterline-schema/lib/waterline-schema/foreignKeys.js:82 throw new Error('Trying to access a collection ' + collection + ' that is ^ Error: Trying to access a collection category_mat that is not defined. at ForeignKeys.findPrimaryKey (/usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/waterline-schema/lib/waterline-schema/foreignKeys.js:82:11) at ForeignKeys.replaceKeys (/usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/waterline-schema/lib/waterline-schema/foreignKeys.js:53:27) at new ForeignKeys (/usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/waterline-schema/lib/waterline-schema/foreignKeys.js:30:10) at new module.exports (/usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/waterline-schema/lib/waterline-schema.js:30:17) at Waterline.initialize (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline.js:106:17) at buildORM (/usr/local/lib/node_modules/sails/lib/hooks/orm/build-orm.js:48:15) at Array.async.auto.instantiatedCollections [as 1] (/usr/local/lib/node_modules/sails/lib/hooks/orm/index.js:191:11) at listener (/usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:465:46) at /usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:419:17 at Array.forEach (native) 

I used this documentation as a reference: http://sailsjs.org/#/documentation/concepts/ORM/Associations/OnetoMany.html

so I don’t know what I am missing, or if I have some kind of configuration ... any help?

+6
source share
1 answer

Perhaps this is because the "category-mat" used in Material.js is not defined anywhere ... try

 // Category_Mat.js module.exports = { identity: 'category_mat', attributes: { name: { type: 'string', required: true }, material:{ collection: 'material', via: 'category' } }, }; 

If this works, the only side effect is that even if you have config / globals.js / models set to true, you won’t be able to access the model in controllers using Category_Mat. You will either have to use "sails.models.category_mat", or just "category_mat".

+1
source

Source: https://habr.com/ru/post/973744/


All Articles