dimanche 11 mars 2018

Rails: Uniqueness Fail if I add_index to the migration file

I have the following migration file "create_preferences.rb":

class CreatePreferences < ActiveRecord::Migration[5.1]
  def change
    create_table :preferences do |t|
      t.integer :user_id
      t.integer :institute_id
      t.integer :preference_value
      t.boolean :accepted

      t.timestamps
    end
    # Jede Student-Insitut Kombination nur einmal
    add_index :preferences, [:user_id, :institute_id], unique: true
  end
end

rails db:migrate:reset runs perfectly, also db:seed does. The problem occurs if I run "rails t" all my tests start to fail. Here are two examples for test errors that I get:

ERROR["test_should_get_about", Minitest::Result, 7.503970391000166]
 test_should_get_about#Minitest::Result (7.50s)
ActiveRecord::RecordNotUnique:         ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: UNIQUE constraint failed: preferences.user_id, preferences.institute_id: INSERT INTO "preferences" ("user_id", "institute_id", "preference_value", "accepted", "created_at", "updated_at", "id") VALUES (1, 1, 1, 'f', '2018-03-11 10:50:19.815172', '2018-03-11 10:50:19.815172', 298486374)


ERROR["test_should_get_home", Minitest::Result, 7.635344781000185]
 test_should_get_home#Minitest::Result (7.64s)
ActiveRecord::RecordNotUnique:         ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: UNIQUE constraint failed: preferences.user_id, preferences.institute_id: INSERT INTO "preferences" ("user_id", "institute_id", "preference_value", "accepted", "created_at", "updated_at", "id") VALUES (1, 1, 1, 'f', '2018-03-11 10:50:19.946527', '2018-03-11 10:50:19.946527', 298486374)

The errors all occur, when I add the

add_index :preferences, [:user_id, :institute_id], unique: true

line to the migration file. Otherwise the tests are fine.

I don't understand that, because some of the 61 failing tests aren't even linked to "preferences".

If I need to add any file, to give you more information, don't hesitate to write a comment. I am not sure, which file would help right now. Maybe the information is relevant, that the validation I added to the model file "preference.rb" didn't fail. The first validates line tests for uniqueness here.

class Preference < ApplicationRecord
  belongs_to :institute
  belongs_to :user

  validates :user_id, uniqueness: {scope: :institute_id}
  validates :user_id, presence: true, numericality: { greater_than_or_equal_to: 0, only_integer: true }
  validates :institute_id, presence: true, numericality: { greater_than_or_equal_to: 0, only_integer: true }
  validates :preference_value, presence: true, numericality: { greater_than_or_equal_to: 1, less_than_or_equal_to: 10, only_integer: true}

end

I am quite lost.

This is the "preferences" schema:

  create_table "preferences", force: :cascade do |t|
    t.integer "user_id"
    t.integer "institute_id"
    t.integer "preference_value"
    t.boolean "accepted"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

This is the seed code, that I use to cretae preferences for all relevant users and the 21 institutes:

  for j in 1..21
    Preference.create!(institute_id: j, user_id: i+4, preference_value: pref.sample)
  end

Thanks for your help!

Aucun commentaire:

Enregistrer un commentaire