Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails migration, references `unique: true` isn't generating `unique: true` in schema causing constancy_fail check to fail

I have the following migration

class CreateBooking < ActiveRecord::Migration[5.1]
  def change
    create_table :bookings do |t|
      t.integer :day_period, default: 0
      t.references :service, foreign_key: true, unique: true, dependent: :destroy, index: true
    end
  end
end

and it generates the following schema:

  create_table "bookings", force: :cascade do |t|
    t.integer "day_period", default: 0
    t.bigint "service_id"
    t.index ["service_id"], name: "index_bookings_on_service_id"
  end

When I run guard which runs a consistency_fail test which fails with:

There are calls to has_one that aren't backed by unique indexes.
----------------------------------------------------------------
Model          Table Columns
----------------------------------------------------------------
Service  bookings (service_id)
----------------------------------------------------------------

Now originally I didn't have unique: true but I rolled back and added it in, still the same problem, again, didn't have index: true so I rolled back and added that in and still the same problem.

I think it's becuase service_id in the schema doesn't have unique: true on it but I don't know and I can't find any information on my specific problem.

What causes this problem, what am I doing that is causing this problem and what can I do to stop this problem, given the current migration?

like image 903
Thermatix Avatar asked Jul 26 '18 09:07

Thermatix


1 Answers

t.references :service, foreign_key: true, dependent: :destroy, index: {unique: true}

More about creating references

like image 186
Leo Avatar answered Nov 15 '22 18:11

Leo