title: Rails migration with unique index, null: false, default: ""
body: <p>I have a <code>subdomain</code> column in my <code>project</code> table. I allow premium users to update the subdomain and free users cannot update it. I also want unique subdomains so that there is no conflict. Currently, I have a setup like this:</p>
  <h3>Project migration</h3>
  <pre class="lang-rb prettyprint-override"><code>create_table :projects, id: uuid do |t|
    t.string :name, null: false, default: &quot;&quot;
    t.string :subdomain, null: false, default: &quot;&quot;
  end
  
  add_index :projects, :subdomain, unique: true
  </code></pre>
  <h3>project.rb (model)</h3>
  <pre class="lang-rb prettyprint-override"><code>validates :subdomain, length: { maximum: 63 }, uniqueness: { case_sensitive: false, allow_blank: true }
  </code></pre>
  <p>The problem is if the free user updates the project <code>table</code>'s <code>name</code> attribute, the <code>subdomain</code> is passed as an empty string as <code>param</code>, and with my current setup, it is not possible for another free user to update the <code>project</code> table as the user would also pass in an empty string for the <code>subdomain</code> column and would result in an error.</p>
  <p>Currently, I am fixing this bug by overwriting the <code>subdomain=</code> method, removing the <code>null: false</code>, and removing the <code>default: &quot;&quot;</code> from the <code>subdomain</code> column</p>
  <pre class="lang-rb prettyprint-override"><code>def subdomain=(value)
    default_value = value.blank? ? nil : value
    super(default_value)
  end
  </code></pre>
  <p>I know I can also overwrite the <code>param</code> hash, but I want to know if there are any other solutions to this. I am willing to change my migration as well.</p>