You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In Rails 7.1 support was added for PostgreSQL unique constraints (rails/rails#46192). This makes it possible to defer checking for constraints inside transactions. This means uniqueness checking can be moved from the index to an unique constraint with add_unique_constraint and this is added to the schema.
Currently rubocop checks the schema for a uniqueness index on the field when a unique validation is set. But it should also accept a unique constraint.
Expected behavior
The unique validation without index cop should not fail when a unique constraint is set on the database column.
Actual behavior
Rails/UniqueValidationWithoutIndex: Uniqueness validation should have a unique index on the database column.
Steps to reproduce the problem
In Rails 7.1 or newer with a PostgreSQL database, create an index for a number field of a model with a deferrable uniqueness constraint.
class DeferModelFieldUniqueness < ActiveRecord::Migration
def change
# Note that we add an index without enforcing uniqueness
add_index :model, :field, name: 'index_models_on_field'
# Ensure the field is unique. Deferrable is set to true to enable changing the field in bulk inside a transaction
add_unique_constraint :model,
:field,
deferrable: :deferred,
name: 'unique_model_field'
end
end
In Rails 7.1 support was added for PostgreSQL unique constraints (rails/rails#46192). This makes it possible to defer checking for constraints inside transactions. This means uniqueness checking can be moved from the index to an unique constraint with add_unique_constraint and this is added to the schema.
Currently rubocop checks the schema for a uniqueness index on the field when a unique validation is set. But it should also accept a unique constraint.
Expected behavior
The unique validation without index cop should not fail when a unique constraint is set on the database column.
Actual behavior
Steps to reproduce the problem
In Rails 7.1 or newer with a PostgreSQL database, create an index for a number field of a model with a deferrable uniqueness constraint.
Add a validation to the model:
RuboCop version
The text was updated successfully, but these errors were encountered: