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

False positive for Rails/NotNullColumn when adding not null constraint to text column ? #952

Closed
j-miyake opened this issue Mar 15, 2023 · 1 comment · Fixed by #1146
Closed
Labels
enhancement New feature or request

Comments

@j-miyake
Copy link
Contributor

Rails/NotNullColumn warns against adding the not null constraint to text column without the default value, however the default value can't be set for text column in MySQL. I'm not sure if this is a bug, but it made me confused. Should we fix this behavior?

C: Rails/NotNullColumn: Do not add a NOT NULL column without a default value.
    add_column :users, :bio, :text, null: false
                                    ^^^^^^^^^^^

Expected behavior

No warnings.

Actual behavior

Get a warning for Rails/NotNullColumn.

Steps to reproduce the problem

Run rubocop against to this migration code.

class AddBioToUsers < ActiveRecord::Migration[7.0]
  def change
    add_column :users, :bio, :text, null: false
  end
end

RuboCop version

$ rubocop -V
1.48.0 (using Parser 3.2.1.1, rubocop-ast 1.27.0, running on ruby 3.2.1) [aarch64-linux]
  - rubocop-performance 1.16.0
  - rubocop-rails 2.18.0
  - rubocop-rake 0.6.0
  - rubocop-rspec 2.19.0
@vlad-pisanov
Copy link
Contributor

Yeah, this issue has come up before; see: #237

It's a database-specific problem. TEXT can have default values in Postgres, but not in MySQL. Since this cop doesn't know which database this migration is for, it simply always enforces a default value.

I suppose we could add a configurable "ignore list" of datatypes, but it might be easier to just disable the cop for that one line.

add_column :users, :bio, :text, null: false # rubocop:disable Rails/NotNullColumn

@koic koic added the enhancement New feature or request label Oct 7, 2023
koic added a commit that referenced this issue Oct 7, 2023
koic added a commit to koic/rubocop-rails that referenced this issue Oct 7, 2023
Fixes rubocop#952.

This PR fixes a false positive for `Rails/NotNullColumn`
when using `null: false` for MySQL's TEXT type.
koic added a commit to koic/rubocop-rails that referenced this issue Oct 7, 2023
Fixes rubocop#952.

This PR fixes a false positive for `Rails/NotNullColumn`
when using `null: false` for MySQL's TEXT type.
koic added a commit to koic/rubocop-rails that referenced this issue Oct 7, 2023
Fixes rubocop#952.

This PR fixes a false positive for `Rails/NotNullColumn`
when using `null: false` for MySQL's TEXT type.
@koic koic closed this as completed in #1146 Oct 9, 2023
koic added a commit that referenced this issue Oct 9, 2023
…_null_column

[Fix #952] Fix a false positive for `Rails/NotNullColumn`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants