6

This is my College model

module.exports = {

  attributes: {
      name:{
        type:'string',
        required:true
      },
      location:{
            type:'string',
            required:true
        },
        faculties:{
            collection:'faculty',
            via:'college'
        }
  }
};

This is my Faculty model

module.exports = {

  attributes: {
        name:{
            type:'string',
            required:true
        },
        description:{
            type:'string'
        },
        college:{
            model:'college',
        required:true
        }
        ,
        years:{
            collection:'year',
            via:'faculty'
        }
  }
};

My problem is that I can add new Faculty with any value in college attribute. If I don't have college with 3000 id, I can still add it but college attribute won't show up when I list all faculties. How can I prevent it from adding a faculty with invalid college id?

Meeker
  • 5,979
  • 2
  • 20
  • 38
Bipin Bhandari
  • 2,694
  • 23
  • 38
  • 1
    You have to set a dominant on your association http://www.sailsjs.org/#!/documentation/concepts/ORM/Associations/Dominance.html – jaumard Apr 29 '15 at 11:01
  • 1
    To prevent adding faculty with invalid college id you need to implement in your model the beforeValidate method and check your college id manually – jaumard Apr 29 '15 at 11:02

1 Answers1

5

Currently waterline does not create foreign key constraints in the manner you describe. It only creates the associated field.

You can use a different library instead of Waterline such as Sequelize.js here is a link about how to go about doing that

https://groups.google.com/forum/#!topic/sailsjs/ALMxbKfnCIo

If your are using a SQL database you can manally create the foreign key constraint yourself.

Or you can validate the value of college before being set in your faculty model by checking in beforeValidate() or afterValidate() on your faculty model.

Meeker
  • 5,979
  • 2
  • 20
  • 38