-
Notifications
You must be signed in to change notification settings - Fork 21.9k
Add configurable deprecation warning for singular associations #45344
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 configurable deprecation warning for singular associations #45344
Conversation
44dbd92
to
944e083
Compare
@@ -79,6 +79,8 @@ def self.configurations | |||
|
|||
class_attribute :strict_loading_by_default, instance_accessor: false, default: false | |||
|
|||
class_attribute :strict_association_name, instance_writer: false, default: false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could go with a single ActiveRecord.allow_deprecated_singular_assocaition_name
(or whatever name you think is good, I didn't think much about it).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Surprised the rails spellchecker complains about association
, but I like allow_deprecated_singular_assocaitions_name
enough I went with it. Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry it wasn't clear. Aside from the name, I was suggesting to make it an ActiveRecord
singleton attribute:
module ActiveRecord
singleton_class.attr_accessor :allow_deprecated_singular_assocaitions_name
end
This means it's no longer inheritable (but the value here is limited), but you can directly check it where it's needed and don't have to pass it around as much. That should significantly cut down the diff.
251b57c
to
e97cc66
Compare
A couple other things:
|
e97cc66
to
147676a
Compare
Thanks @byroot, I have never done configuration variables so I am guessing some of these based on other PRs I was able to find. Please let me know if I got anything wrong. |
activerecord/CHANGELOG.md
Outdated
record within a transaction. | ||
|
||
When multiple Active Record instances change the same record within a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gosh, I gotta disable my "cleanup whitespace" config.
147676a
to
58937a8
Compare
0c5a5e5
to
90b57b6
Compare
@@ -79,6 +79,8 @@ def self.configurations | |||
|
|||
class_attribute :strict_loading_by_default, instance_accessor: false, default: false | |||
|
|||
class_attribute :allow_deprecated_singular_associations_name, instance_writer: false, default: true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still think we should make this a global on ::ActiveRecord
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree configuring this per class seems like a really bad idea. Updated thanks!
@@ -44,3 +44,6 @@ | |||
# For example, it is possible to create an index for a non existing column. | |||
# See https://www.sqlite.org/quirks.html#double_quoted_string_literals_are_accepted for more details. | |||
# Rails.application.config.active_record.sqlite3_adapter_strict_strings_by_default = true | |||
|
|||
# Disable deprecated singular associations names | |||
# Rails.application.config.active_record.allow_deprecated_singular_associations_name = true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need a newline :)
@@ -288,6 +289,12 @@ def load_defaults(target_version) | |||
if respond_to?(:active_record) | |||
active_record.sqlite3_adapter_strict_strings_by_default = true | |||
end | |||
when "7.2" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be in 7.1
defaults, since 7.1 isn't out yet.
75444c5
to
c947fb6
Compare
This removes the singularize from `where` which runs on all `expand_from_hash` keys which might be reflections or column names. This saves a lot of time by avoiding singularizing column names. Previously in rails#45163 the singularize was removed entirely. after some reflection, I think it is better to at least give a warning for one release since `where` is a very popular API and the problems you can run into with incorrect relation could be hard to debug. Configurable with `ActiveRecord::Base.allow_deprecated_singular_assocaitions_name = false` / `config.active_record.allow_deprecated_singular_assocaitions_name = false`
c947fb6
to
3a04c7b
Compare
phew, thanks so much @byroot I know so much more about how configuration variables work 😅 |
Thanks! |
…me [skip ci] The only time rails#45163 and rails#45344 have an effect is when the hash value passed to `where` is a model object. The current sample code does not change behavior between Rails 7.0 and 7.1
Follow-up to rails#45344. This tweaks the description, fleshes out the code example, and fixes the default value listed for 7.1.
Follow-up to rails#45344. When using `config.load_default "7.1"`, the value is `false`, so the value in `new_framework_defaults_7_1.rb` should be `false` as well. This commit also adds configuration tests.
Follow-up to rails#45344. Currently, when `allow_deprecated_singular_associations_name` is true, a deprecation warning like the following is displayed: ```console > Comment.belongs_to :post > Comment.where(posts: 1).count DEPRECATION WARNING: In Rails 7.2, referring to singular associations by their plural name will be deprecated. To continue querying `post` use `posts` instead. You can get the new more performant behavior now by setting config.active_record.allow_deprecated_singular_associations_name = false ``` The warning advises "use `posts` instead", but the user's code should actually use `post`, i.e. `Comment.where(post: 1).count`. The mismatch is likely due to the level at which the deprecation warning is emitted. `associated_with?` does indeed expect `table_name` to be `posts`, but `where` expects `post`. This commit changes the deprecation warning to avoid advising a specific usage, but still be explicit about what is wrong: ```console > Comment.where(posts: 1).count DEPRECATION WARNING: Referring to a singular association (e.g. `post`) by its plural name (e.g. `posts`) is deprecated. To convert this deprecation warning to an error and enable more performant behavior, set config.active_record.allow_deprecated_singular_associations_name = false. ```
This removes the singularize from
where
which runs on allexpand_from_hash
keys which might be reflections or column names. This saves a lot of time by avoiding singularizing column names.Previously in #45163 the singularize was removed entirely. after some reflection, I think it is better to at least give a warning for one release since
where
is a very popular API and the problems you can run into with incorrect relation could be hard to debug.Configurable with
ActiveRecord::Base.allow_deprecated_singular_assocaitions_name = false
/config.active_record.allow_deprecated_singular_assocaitions_name = false
cc: @byroot sorry for the flip flop. I was imaging I didn't know about this change and things just started acting different and felt concerned for that rails user.