Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Foreign key constraint on SailsJS

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?

like image 557
Bipin Bhandari Avatar asked Apr 29 '15 10:04

Bipin Bhandari


1 Answers

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.

like image 133
Meeker Avatar answered Sep 16 '22 15:09

Meeker