Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Waterline, error when trying to create 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 app 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 reference: http://sailsjs.org/#/documentation/concepts/ORM/Associations/OnetoMany.html

so I don't know what I'm missing or if there is a configuration that I have to do... any help?

like image 551
arielmcm Avatar asked Aug 13 '14 03:08

arielmcm


1 Answers

Maybe it is because "category-mat" used on 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 the controllers by using "Category_Mat". You will either have to use "sails.models.category_mat" or just "category_mat".

like image 107
Stenio Ferreira Avatar answered Jan 02 '23 12:01

Stenio Ferreira