gpt4 book ai didi

node.js - Sails.js 模型 : create 2 association to self failed

转载 作者:行者123 更新时间:2023-12-02 16:29:51 24 4
gpt4 key购买 nike

我对 Nodejs 和 sails 还很陌生。我正在实现一个类似于 Twitter 的服务器。在用户模型中,应该有2个字段:follower和following,这2个字段是模型“用户”本身的关联。

我的问题是,当模型只有 1 个关联(关注者或关注者)时,它会起作用。但是当follower和following都包含时,就会报错。

代码是这样的:

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 关键字。根据waterline associations docs via 关键字引用关联的另一端。因此,对于追随者来说,另一方是跟随,反之亦然。换句话说:

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

关于node.js - Sails.js 模型 : create 2 association to self failed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30301057/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com