title: FOREIGN KEY ON DELETE SET NULL in rails
body: <p>I'm working with two models in rails</p>
  
  <pre><code>    class Student &lt; ApplicationRecord
        belongs_to :school
        has_many :lessons
      end
  </code></pre>
  
  <p>and </p>
  
  <pre><code>    class Lesson &lt; ApplicationRecord
        belongs_to :student, optional: true
      end
  </code></pre>
  
  <p>I was really hoping this would result in what Oracle does with FOREIGN KEY ON DELETE SET NULL</p>
  
  <p>after I do rails db:migrate:reset
  I get a generated db/schema.rb with the line</p>
  
  <pre><code>      add_foreign_key "lessons", "students"
  </code></pre>
  
  <p>Which is not what I was expecting given my models, so
  I modified it by hand to </p>
  
  <pre><code>   add_foreign_key "lessons", "students", on_delete: :nullify
  </code></pre>
  
  <p>No matter what I do, I get a foreign key violation every time I try to delete a student who has lessons. </p>
  
  <p>Why doesn't the schema.rb reflect the models in the first place?</p>
  
  <p>Why don't the tables reflect the changes I made to schema.rb by hand?</p>
  
  <p>How do I delete a student without deleting all the lessons?</p>