Sails.js模型:创build2个关联自失败

我很新的Nodejs和帆。 我正在实现一个类似于Twitter的服务器。 在用户模型中,应该有两个字段:follower和follow,这两个字段是模型“用户”本身的关联。

我的问题是,当模型只有1个关联,追随者或追随者,它的工作。 但是,当追随者和追随者都包括在内时,就会出现错误。

代码是这样的:

module.exports = { attributes: { alias: { type:'string', required: true, primaryKey: true }, pwd: { type: 'string', required: true }, follower: { collection: 'user', via: 'alias' }, following:{ collection: 'user', via: 'alias' } } 

该代码将导致这样的错误:

  usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/waterline-schema/lib/waterline-schema/references.js:115 throw new Error('Trying to associate a collection attribute to a model tha ^ Error: Trying to associate a collection attribute to a model that doesn't have a Foreign Key. user is trying to reference a foreign key in user at References.findReference (/usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/waterline-schema/lib/waterline-schema/references.js:115:11) at References.addKeys (/usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/waterline-schema/lib/waterline-schema/references.js:72:22) 

对于这种用法,您的模型定义是不正确的,即via关键字。 根据水线关联文档 , via关键字引用关联的另一侧。 所以,对于follower ,另一方是follower ,反之亦然。 换一种说法:

 follower: { collection: 'user', via: 'following' }, following:{ collection: 'user', via: 'follower' } 

你可以在https://github.com/appscot/sails-orientdb/blob/master/test/integration-orientdb/tests/associations/manyToMany.selfReferencing.js查看完整的工作示例