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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With