2

I have two models declared in Sails and I'm using the Waterline-Orientdb adapter and don't know how to connect them via a bi-directional edge

Questions Model

var Waterline = require('waterline');

module.exports = Waterline.Collection.extend({

  tableName: 'questionsTable',
  identity: 'questions',
  connection: 'associations',

  attributes: {
    id: { type: 'string', primaryKey: true, columnName: '@rid'},
    question  : { type: 'string'},
    user: { model: "User", required: true },
    answerOptions: {type: 'json'},
    imagefile: {type:'string'},
    answers: {
      collection: 'answer',
      via: 'questions',
      dominant:true
    }
  }

});

Answer Model

var Waterline = require('waterline');

module.exports = Waterline.Collection.extend({

    tableName: 'answerTable',
    identity: 'answer',
    connection: 'associations',

    attributes: {
        id: {
            type: 'string',
            primaryKey: true,
            columnName: '@rid'
        },
        Answer: {
            type: 'string'
        },
        questions: {
            collection: 'questions',
            via: 'answer'
        }

    }
});

I want to be able to create an edge between the two models. The user creates a question and then users can post a response.

Dário
  • 2,002
  • 1
  • 18
  • 28
TommyK
  • 416
  • 2
  • 6
  • 24
  • A question can have many answers but an answer can have only one question, seems you need to change the questions tag in answer to `question { model: 'question' }` – arkoak Mar 11 '15 at 11:39
  • That is true. But say i wanted to model this as many-to-many and wanted an edge. How would i accomplish that? – TommyK Mar 11 '15 at 11:55

1 Answers1

1

There is a typo on your answer model:

questions: {
  collection: 'questions',
  via: 'answer'
}

should be

questions: {
  collection: 'questions',
  via: 'answers'  // answers as that is the attribute name in questions model
}

Example to create questions, answers and then link them:

var question1;
ontology.collections.questions.create({ question: 'question1' })
  .then(function(question){
    question1 = question;

    return ontology.collections.answer.create([{ answer: 'answer1' }, { answer: 'answer2' }]);
  })
  .then(function(answers){
    question1.answers.add(answers[0]);
    question1.answers.add(answers[1]);

    return question1.save();
  })

I've created a running example at github.com/appscot/waterline-orientdb.

Regards

UPDATE: waterline-orientdb is now named sails-orientdb.

Dário
  • 2,002
  • 1
  • 18
  • 28