Sails.js model: create 2 associations with an error

I am new to Nodejs and sails. I am implementing a server similar to Twitter. The user model must have 2 fields: the successor and the next, and 2 fields - this is the association of the user of the model.

My question is that the model has only 1 association, either a follower or the next one, it works. However, when both followers and subsequent ones are included, there will be an en error.

The code looks something like this:

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' } } 

The code causes this error:

  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) 
+6
source share
1 answer

For this use, your model definition is incorrect, namely the via keywords. According to the waterline docs association , the via keyword refers to the other side of the association. So, for a follower other side is following and vice versa. In other words:

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

You can check the complete working example: https://github.com/appscot/sails-orientdb/blob/master/test/integration-orientdb/tests/associations/manyToMany.selfReferencing.js

+3
source

Source: https://habr.com/ru/post/986397/


All Articles