You came here to fix Rails/UniqueValidationWithoutIndex validate-uniqueness-on-db

rails g scaffold inbox name
rails g scaffold message inbox:references body:text

1. Validate uniqueness #

# app/models/inbox.rb
  validates :name, uniqueness: true
class CreateInboxes < ActiveRecord::Migration[7.0]
  def change
    create_table :inboxes do |t|
      t.string :name, null: false
++    t.string :name, null: false, index: { unique: true }

      t.timestamps
    end
++  # add_index :messages, :body, unique: true
  end
end
# app/models/message.rb
  validates :body, uniqueness: { scope: :inbox_id }

2. Validate uniqueness scope #

class CreateMessages < ActiveRecord::Migration[7.0]
  def change
    create_table :messages do |t|
      t.references :inbox, null: false, foreign_key: true
      t.text :body, null: false

      t.timestamps
    end
++  add_index :messages, [:body, :inbox_id], unique: true
  end
end