0

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!

Mostah
  • 25
  • 7
  • this is not the best solution, but sometimes you just drop the whole test db or you just run the correct `rake` command to `prepare` the test db https://stackoverflow.com/questions/15169894/what-does-rake-dbtestprepare-actually-do#15170001 without going to much in the real issue detail, which would take time. If we want to go into the detail, I believe your issue is connected to the db settings you are doing, which require the db to validate the data creation. Usually best practice in rails is not using validation on db side or on the front end side, but we use validations in the model – Fabrizio Bertoglio Mar 11 '18 at 12:25
  • using a validation in the model could be a workaround with many advantages, otherwise understand why the record you are creating are not unique with this two field `:preferences, [:user_id, :institute_id]` and correct your `stubs` or `factories` – Fabrizio Bertoglio Mar 11 '18 at 12:26
  • tried `rake db:test:prepare` ? other question to all: is it ok to create index without specifying a specific name ? – Simon Franzen Mar 11 '18 at 13:10
  • And I agree to @FabrizioBertoglio that defining a lot if `null: false` for tables can bring you in trouble. – Simon Franzen Mar 11 '18 at 13:12

0 Answers0