Ren*_*ang 6 node.js sails.js waterline sails-mongo
我在Nodejs和sails上都很新.我正在实现一个类似于Twitter的服务器.在用户模型中,应该有2个字段:跟随者和跟随者,并且2个字段是模型"用户"本身的关联.
我的问题是当模型只有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'
}
}
Run Code Online (Sandbox Code Playgroud)
代码会导致这样的错误:
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)
Run Code Online (Sandbox Code Playgroud)
对于这种用法,您的模型定义(即关键字)不正确via。根据水线关联文档,关键字via引用了关联的另一侧。所以,对于 a 来说follower另一边是following,反之亦然。换句话说:
follower: {
collection: 'user',
via: 'following'
},
following:{
collection: 'user',
via: 'follower'
}
Run Code Online (Sandbox Code Playgroud)
您可以在以下位置查看完整的工作示例:https://github.com/appscot/sails-orientdb/blob/master/test/integration-orientdb/tests/associations/manyToMany.selfReferencing.js
| 归档时间: |
|
| 查看次数: |
2052 次 |
| 最近记录: |