水线,尝试创建一对多关联时出错

时间:2014-08-13 03:26:52

标签: sails.js waterline

我有这些模特:

// Material.js

module.exports = {
attributes: {

    name: {
        type: 'string',
        required: true
    },
    source_info: {
        type: 'string',
        required: true
    },
    category: { model: 'category_mat' }
}
};

// Category_Mat.js

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

但是当我运行应用程序时,我收到此错误:

/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)

我使用此文档作为参考: http://sailsjs.org/#/documentation/concepts/ORM/Associations/OnetoMany.html

所以我不知道我错过了什么,或者是否有我必须做的配置......有什么帮助吗?

1 个答案:

答案 0 :(得分:1)

也许是因为Material.js上使用的“category-mat”没有在任何地方定义...尝试

// Category_Mat.js

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

如果这样可行,唯一的副作用是即使您将config / globals.js / models设置为“true”,您也无法使用“Category_Mat”访问控制器中的模型。您将不得不使用“sails.models.category_mat”或“category_mat”。