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
Use alias_attribute
to provide id_value
alias for id
attribute
#48930
Conversation
I have a few concerns with this.
There are also plenty of other names that our predefined methods "reserve" -- we're surely not going to also predefine a It is our well-established convention that Finally, guaranteeing that every model has an alias defined prevents us from ever making performance optimizations for the extremely common case where a model does not have any aliases in use. |
👋 Hey @matthewd , thanks for your feedback!
That's fair. We threw around some different options What we'd like to achieve here is providing applications with access to the raw
Also fair. We could provide a simple getter / setter for the id column that would access the raw column without using an alias, though then we don't get the Anyways, I'll let Rafael weight in -- we can continue aliasing individual attributes in our application, so this isn't a blocker. |
Although other reserved columns don't have readers and setters, it is totally possible to access them using I believe renaming columns is out of question. Rails for years recommended, and still recommend people to create tables with an In my opinion this is a decision the framework needs to make if it is to add support to composite primary key. If I was going to design this from scratch I'd never have I know this method will be another special method, but again, we can't design this properly given the restrictions. Any other name we though is worse than |
Given it's only part of the primary key, it is definitionally not unique. An application might do something more specific, but we can't imply that's reasonable to assume everywhere. By the same logic, it's not an object identifier. I think I still think this is best addressed in-app, where one can apply a more contextually-aware name (e.g. if you know that the I definitely don't think the method(s)/alias should be there if there isn't an |
Okay, to summarize the feedback I'm hearing:
My only counter to the latter part of this statement is that it would prevent us from being able to use the alias/method on an object that may or may not have Unless there's further feedback, my next steps will be to
|
I agree with that.
I don't think this is an "application" concern. It is a framework design concern. The framework that should never had done the decision to make The framework is officially going to support composite primary keys, but it feels like this feature isn't really officially supported. Users of it are on their own to figure out issues like this. They will probably find this issue and ask "Hey, why I can't get the value of the We could document this case, but as a framework designer I'd not feel good in writing documentation that basically means: Hey, we screw up in the past, and you are the ones that will have to do work to fix my mistake. We could do this properly as well an deprecate Maybe a per-model, allowing to be per app, configuration that means "I have no custom primary keys in my app, don't should this deprecation to me" would be good enough.
A deprecation like this is tricky because there is no way to mark valid usage of |
# Provides access to the value of the record's id column. | ||
# Use +uid+ over +id+ if the raw id column value is needed, | ||
# rather than the value of the primary key. | ||
alias_attribute :uid, :id |
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 this is implemented in wrong place. It should be called in the same place that we call attribute :name
for example. (I believe it is in the schema loader)
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 wasn't entirely sure where you were suggesting this be moved 😅 I moved it to #define_attribute
, since we define all of the attributes for a model when load_schema!
is called. Made sense that we'd register the alias when defining the id
attribute. Let me know what you think.
@rafaelfranca can you clarify whether you're in favour of having this alias show up for all models with an id column, or only for models where the PK is not
I was envisioning that the deprecation would only show up if |
251bddf
to
c025e7f
Compare
In April I explored what it would look like to deprecate the primary key as FWIW I think my branch goes too far but it at least gives an idea of how invasive this is. |
@adrianna-chang-shopify for all models with an |
alias_attribute
to provide uid
alias for id
attributealias_attribute
to provide id_value
alias for id
attribute
This allows access to the raw id column value on records for which an id column exists but is not the primary key. This is common amongst models with composite primary keys.
c025e7f
to
e90b11e
Compare
Motivation / Background
Extends on #48533.
This allows access to the raw
id
column value on records for which an id column exists but is not the primary key. This is common amongst models with composite primary keys.Detail
Use
alias_attribute
to provide auid
alias for theid
attribute. That was one of the primary use cases described in the above pull request; as such, we've decided to provide it out of the box in Rails.Additional information
Is there a better way to provide documentation for
#uid
than via a comment inserted above thealias_attribute
call?Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]
cc @nvasilevski @rafaelfranca