简体   繁体   English

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

[英]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 我使用此文档作为参考: 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? 所以我不知道我错过了什么,或者是否有我必须做的配置...任何帮助?

Maybe it is because "category-mat" used on Material.js is not defined anywhere... try 也许是因为Material.js上使用的“category-mat”没有在任何地方定义...试试

// 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". 如果这有效,唯一的副作用是即使您将config / globals.js / models设置为“true”,您也无法使用“Category_Mat”访问控制器中的模型。 You will either have to use "sails.models.category_mat" or just "category_mat". 您将不得不使用“sails.models.category_mat”或“category_mat”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM