title: How to add reference column migration in Rails 6 with SQLite
body: <p>What is the correct way to add reference column migration in Rails 6 without getting <code>SQLite3::SQLException: Cannot add a NOT NULL column with default value NULL</code>?</p>
  
  <p>I can hack it to get it working; but, I am preparing a tutorial for a grad class, so I want to make sure I'm doing it "by the book".</p>
  
  <p>The starting point is a <code>Post</code> class (think "blog post").  I want to add an <code>Author</code> class and set up an 1-to-many relationship between authors and posts. After adding the <code>author</code> class and running the corresponding migration, I then create a migration to add an <code>Author</code> reference to <code>Post</code>:</p>
  
  <p><code>rails g migration AddAuthorToPost author:references</code></p>
  
  <p>This command generates:</p>
  
  <pre><code>class AddAuthorToPost &lt; ActiveRecord::Migration[6.0]
    def change
      add_reference :posts, :author, null: false, foreign_key: true
    end
  end
  </code></pre>
  
  <p>The problem is, of course, that SQLite complains because it won't tolerate the potential for a <code>null</code> foreign key --- even if the <code>Post</code> table is empty: (<a href="https://stackoverflow.com/questions/3170634/how-to-solve-cannot-add-a-not-null-column-with-default-value-null-in-sqlite3">How to solve &quot;Cannot add a NOT NULL column with default value NULL&quot; in SQLite3?</a>)</p>
  
  <p>I looked back at the previous year's tutorial (prepared by a different instructor) and the generator did not add <code>null: false</code> to the migration. (See also <a href="https://stackoverflow.com/questions/39937839/add-a-reference-column-migration-in-rails-5">Add a reference column migration in Rails 5</a>)</p>
  
  <p>Removing <code>null: false</code> from the migration allows the migration to run; but, "disabling safety features" doesn't seem appropriate in a classroom setting :)</p>
  
  <p>Is there a better way to do this?</p>