-
-
Notifications
You must be signed in to change notification settings - Fork 161
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
Fetch attribute by alias #524
Comments
This can be hidden by your relation API. That's the whole point of having this layer - hide details. I'm not sure if there's anything we should do about it to be honest, but maybe I'm wrong.
Why do you think that? It's very important to have a method that returns attributes without any ambiguity. Adding the second lookup to Take this as an example why it's important for [7] pry(main)> users.join(tasks).select(*users.schema, *tasks.schema.rename(title: :name)).schema.map(&:name)
=> [:id, :name, :id, :user_id, :title]
[8] pry(main)> users.join(tasks).select(*users.schema, *tasks.schema.rename(title: :name)).schema[:title]
=> #<ROM::SQL::Attribute[NilClass | String] name=:title source=ROM::Relation::Name(tasks) qualified=true sql_expr=#<Sequel::SQL::AliasedExpression @expression=>#<Sequel::SQL::QualifiedIdentifier @table=>:tasks, @column=>:title>, @alias=>:name, @columns=>nil> alias=:name>
[9] pry(main)> users.join(tasks).select(*users.schema, *tasks.schema.rename(title: :name)).schema[:name]
=> #<ROM::SQL::Attribute[NilClass | String] name=:name source=ROM::Relation::Name(users) qualified=true sql_expr=#<Sequel::SQL::QualifiedIdentifier @table=>:users, @column=>:name> alias=nil> Now imagine if |
Thanks for taking this into consideration.
It is fair. The difference with my view is that I'd like to be more granular and hide it in the schema sub-layer within the relation layer. My interest with it comes because I have been working with a legacy database with extremely ugly names, mixing even different languages in the column names. For this reason, I'd like to finish with the pain in my eyes in one single line: attribute :i_m_super_ugly, alias: :i_m_so_nice From this moment on, I'd like to be possible not to use Then there is also some cross-layer coupling I have been using because sometimes it is too convenient to avoid. For example, say you are rendering an HTML table containing data from a relation and this table have links to be ordered by any of the columns, where each column represents a relation attribute. Currently I'm using the attribute name to build the link, and it is travelling unmodified from the view layer bottom to the relation layer, where it finishes its voyage within
I think it is similar to what currently is happening with this slightly modified example: [1] pry(main)> users.join(tasks).select(*users.schema, *tasks.schema).schema.map(&:name)
=> [:id, :name, :id, :user_id, :name]
[2] pry(main)> users.join(tasks).select(*users.schema, *tasks.schema).schema[:name] Right now it is returning first-match. With my original idea, |
I reported #529 about this
Actually, this may work. It's worth experimenting with this approach. |
I need this too because I have to use it with lots of legacy databases. I would like to use the aliased attribute in all my database interactions like was done in the datamapper days. in my associations, in my joins, in everything |
@dsisnero we'll get to this eventually! |
As you know from previous issues and PR's, I'm trying to make rom a complete isolation layer from the column names used in the persistence layer (besides the configuration of the scheme, of course). For this reason, I would like to be able to fetch in the most transparent possible way an attribute by its aliased name.
Examples
Right now, if you have:
you can do
relation[:foo]
but notrelation[:bar]
.This is very annoying when building queries, because you have to remember to use the persistence layer name each time you use an aliased attribute.
Ideally, I would like to add steroids to the
#[]
method so that it does a second lookup by alias if nothing is found with given name. However, I still like the idea of having a more pure method which only finds by attribute name, which I think should be used mainly internally in rom implementation.We could do two things here:
#[]
to lookup by name and alias and use another#fetch_by_name
internally.#[]
as it is and add another method#fetch_with_alias
or just#>>
which would lookup by name and alias. However, from the user point of view, recommending using both#[]
and#fetch_with_alias
would not be transparent and would make the code harder to read (because#[]
is very convenient).The text was updated successfully, but these errors were encountered: