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

Add check for add_reference method by Rails/NotNullColumn cop #3782

Merged

Conversation

pocke
Copy link
Collaborator

@pocke pocke commented Dec 11, 2016

add_reference method has same problem as add_column method(see #3415 ). So, Rails/NotNullColum cop also should detect the method.

For example

class SomeMigration < ActiveRecord::Migration
  def change
    add_reference :products, :category, null: false
  end
end

The above migration crashes other than MySQL.
Then, in MySQL, the migration does not crash, but category_id is decided by MySQL without permission.


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Used the same coding conventions as the rest of the project.
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • All tests are passing.
  • The new code doesn't generate RuboCop offenses.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Updated cop documentation with rake generate_cops_documentation (required only when you've added a new cop or changed the configuration/documentation of an existing cop).

`add_reference` method has same problem as `add_column` method.

```ruby
class SomeMigration < ActiveRecord::Migration
  def change
    add_reference :products, :category, null: false
  end
end
```

The above migration crashes other than MySQL.
Then, in MySQL, the migration does not crash, but `category_id` is decided by MySQL without permission.
So, RuboCop also should detect `add_reference` method usage.
@pocke pocke force-pushed the Rails/NotNullColumn-cop-support-add-references branch from 54c770b to 9b6c0f2 Compare December 11, 2016 14:55
@bbatsov bbatsov merged commit 456c6d7 into rubocop:master Dec 12, 2016
@bbatsov
Copy link
Collaborator

bbatsov commented Dec 12, 2016

👍

@pocke pocke deleted the Rails/NotNullColumn-cop-support-add-references branch December 12, 2016 23:01
@md5
Copy link

md5 commented Oct 16, 2017

I'm not sure I understand why this change was made. How is it possible to have a default value for a reference column? By definition it's going to refer to the primary key of some other table, so it can't have a default.

@pocke
Copy link
Collaborator Author

pocke commented Oct 16, 2017

By definition it's going to refer to the primary key of some other table, so it can't have a default.

The added reference column can have a default value.
For example:

default_category Category.create!(name: 'default')
add_reference :products, :category, null: false, default: default_category.id

Or we should make the reference to allow null.

add_reference :products, :category, null: true

@md5
Copy link

md5 commented Oct 16, 2017

Fair enough, but that seems like an uncommon case. I would think it's more common to not known the default for a non-null reference. The example in the documentation of hard-coding the default to 1 certainly doesn't make sense.

@pocke
Copy link
Collaborator Author

pocke commented Oct 18, 2017

The example in the documentation of hard-coding the default to 1 certainly doesn't make sense.

Do you mention this https://github.com/bbatsov/rubocop/pull/3782/files#diff-981212d9d67b8b33a445cb8321ae326dR18 ?
This example doesn't make sense. And I think we can update the documentation with the following.

add_reference :products, :category
# or
add_reference :products, :category, null: false, default: Category::DEFAULT_CATEGORY_ID
# or
add_reference :products, :category
Products.find_each do |product|
  product.set_category
  product.save!
end
change_column_null :products, :category_id, false

@tgaff
Copy link

tgaff commented Jul 17, 2020

This seems like more of the exception than the rule.
It's common to want to ensure that an id is always set but much less common to want it to point at a default rather than cause an error. A bank account definitely needs an owner_id, but I'd rather have it fail to open the account than point at the bank's president by default.

The non-references portion of this rule looks useful. Is there any way to configure this to not check add_reference but keep the rest of the rule?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants