Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error in Rails/UniqueValidationWithoutIndex #214

Closed
seandilda opened this issue Mar 24, 2020 · 6 comments · Fixed by #218
Closed

error in Rails/UniqueValidationWithoutIndex #214

seandilda opened this issue Mar 24, 2020 · 6 comments · Fixed by #218
Labels
bug Something isn't working

Comments

@seandilda
Copy link


Expected behavior

To not raise an exception

Actual behavior

Here's the backtrace generated by running rubocop -d <filename>:

Inspecting 1 file
Scanning /usr/src/app/app/models/broken_model.rb
An error occurred while Rails/UniqueValidationWithoutIndex cop was inspecting /usr/src/app/app/models/broken_model.rb:2:2.
undefined method `type' for nil:NilClass
/bundle/ruby/2.6.0/gems/rubocop-rails-2.5.0/lib/rubocop/rails/schema_loader/schema.rb:82:in `each_content'
/bundle/ruby/2.6.0/gems/rubocop-rails-2.5.0/lib/rubocop/rails/schema_loader/schema.rb:62:in `each'
/bundle/ruby/2.6.0/gems/rubocop-rails-2.5.0/lib/rubocop/rails/schema_loader/schema.rb:62:in `map'
/bundle/ruby/2.6.0/gems/rubocop-rails-2.5.0/lib/rubocop/rails/schema_loader/schema.rb:62:in `build_columns'
/bundle/ruby/2.6.0/gems/rubocop-rails-2.5.0/lib/rubocop/rails/schema_loader/schema.rb:51:in `initialize'
/bundle/ruby/2.6.0/gems/rubocop-rails-2.5.0/lib/rubocop/rails/schema_loader/schema.rb:27:in `new'
/bundle/ruby/2.6.0/gems/rubocop-rails-2.5.0/lib/rubocop/rails/schema_loader/schema.rb:27:in `block in build!'

/bundle/ruby/2.6.0/gems/rubocop-rails-2.5.0/lib/rubocop/rails/schema_loader/schema.rb:37:in `block in each_table'
/bundle/ruby/2.6.0/gems/rubocop-rails-2.5.0/lib/rubocop/rails/schema_loader/schema.rb:34:in `each'
/bundle/ruby/2.6.0/gems/rubocop-rails-2.5.0/lib/rubocop/rails/schema_loader/schema.rb:34:in `each_table'
/bundle/ruby/2.6.0/gems/rubocop-rails-2.5.0/lib/rubocop/rails/schema_loader/schema.rb:26:in `build!'
/bundle/ruby/2.6.0/gems/rubocop-rails-2.5.0/lib/rubocop/rails/schema_loader/schema.rb:12:in `initialize'
/bundle/ruby/2.6.0/gems/rubocop-rails-2.5.0/lib/rubocop/rails/schema_loader.rb:34:in `new'
/bundle/ruby/2.6.0/gems/rubocop-rails-2.5.0/lib/rubocop/rails/schema_loader.rb:34:in `load!'
/bundle/ruby/2.6.0/gems/rubocop-rails-2.5.0/lib/rubocop/rails/schema_loader.rb:18:in `load'
/bundle/ruby/2.6.0/gems/rubocop-rails-2.5.0/lib/rubocop/cop/rails/unique_validation_without_index.rb:128:in `schema'
/bundle/ruby/2.6.0/gems/rubocop-rails-2.5.0/lib/rubocop/cop/rails/unique_validation_without_index.rb:35:in `on_send'
/bundle/ruby/2.6.0/gems/rubocop-0.80.1/lib/rubocop/cop/commissioner.rb:57:in `block (2 levels) in trigger_responding_cops'

Steps to reproduce the problem

Here's the sample model I'm using:

class BrokenModel < ApplicationRecord
  validates :name, uniqueness: { scope: :second_attribute }
end

RuboCop version

Include the output of rubocop -V or bundle exec rubocop -V if using Bundler. Here's an example:

$ rubocop -V
0.80.1 (using Parser 2.7.0.5, running on ruby 2.6.5 x86_64-linux)
@koic
Copy link
Member

koic commented Mar 24, 2020

Thanks for the feedback. Can you show me db/schema.rb?

@FraDim
Copy link

FraDim commented Mar 24, 2020

@koic: I get the same error. In my case, it is due to a table that only has a primary key (no other columns). Here is the relevant part of the db/schema.rb

create_table "data_migrations", primary_key: "version", id: :string, force: :cascade do |t|
end

@seandilda
Copy link
Author

That particular model is not in the schema.

I originally hit this bug with a model called Building::Location and here its its schema entry:

  create_table "building_locations", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t|
    t.integer "building_id", null: false
    t.string "name", null: false
    t.integer "building_floor_plan_id"
    t.decimal "latitude", precision: 16, scale: 13
    t.decimal "longitude", precision: 16, scale: 13
    t.string "map_type"
    t.string "map_file_name"
    t.binary "map_data", limit: 16777215
    t.text "notes"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.index ["building_floor_plan_id"], name: "index_building_locations_on_building_floor_plan_id"
    t.index ["building_id", "name"], name: "index_building_locations_on_building_id_and_name", unique: true
end

I also hit it on a model called device with the following validates line:

  validates :management_ip, format: { with: Resolv::IPv4::Regex }, uniqueness: true,
                            allow_nil: true, if: :management_ip_changed?

@FraDim
Copy link

FraDim commented Mar 24, 2020

Apologies @seandilda, I didn't mean to overtake this issue – just got overly excited that someone posted about the issue I was having, minutes before I went looking for it.

You seem to have a completely different bug than me.

@seandilda
Copy link
Author

@FraDim no worries. Its still possible they're the same underlying issue.

@koic koic added the bug Something isn't working label Mar 26, 2020
koic added a commit to koic/rubocop-rails that referenced this issue Mar 26, 2020
Fixes rubocop#214.

This PR fixes an error for `Rails/UniqueValidationWithoutIndex`
when a table has no column definition.
@koic
Copy link
Member

koic commented Mar 26, 2020

I reproduced the error and opened PR #218. Thank you!

koic added a commit to koic/rubocop-rails that referenced this issue Mar 27, 2020
Fixes rubocop#214.

This PR fixes an error for `Rails/UniqueValidationWithoutIndex`
when a table has no column definition.
@koic koic closed this as completed in #218 Mar 29, 2020
koic added a commit that referenced this issue Mar 29, 2020
…hout_index

[Fix #214] Fix an error for `Rails/UniqueValidationWithoutIndex`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants