jeudi 15 janvier 2015

Sqlite Rails foreign key : where in rails can i write something like FOREIGN KEY(User_id) REFERENCES User(id)

I have 2 modules in my Rails app, School and Students, obviously, School has many students and Student belongs to School.



class School < ActiveRecord::Base
has_many :students

class Student < ActiveRecord::Base
belongs_to :school


i used to understand that this means that rails will search for a column named school_id in the Student table which is a foreign key for the school..


the thing is if i entered a new student with id 6 for example and their is no school with this id, rails will create the entry without any errors or problem !!


is there is a certain place where i should declare that FOREIGN KEY(school_id) REFERENCES School(id)


thanks in advance :)


Aucun commentaire:

Enregistrer un commentaire