Em 26-04-2012 18:54, Jeremy Evans escreveu:
On Thursday, April 26, 2012 1:12:33 PM UTC-7, Rodrigo Rosenfeld Rosas
wrote:
Em 26-04-2012 15:12, Jeremy Evans escreveu:
On Thursday, April 26, 2012 10:24:25 AM UTC-7, Rodrigo Rosenfeld
Rosas wrote:
Sometimes we want to do something like:
Page.eager_graph(:user).select(:pages__name, :users__name).first
But this relies on the fact that we know the table names for
our Sequel Models Page and User. What if they're set up this way?
Do you honestly have an app where you don't know the table names
being used?
No, that is not the issue. I'm dealing with a legacy database that
wasn't designed by me. I don't want to remember the the user's
table is called "jsec_user". I'd prefer to just handle with my ORM
without remembering the details of my database schema.
Don't be so lazy. :) Sequel was not designed to abstract such
details. Really, if you don't want to remember details in your
database, such as table and column names, Sequel is probably not the
right tool for you.
This is not about laziness. In a far future I hope to get all my
application converted from Grails to Rails. After that I'd like to write
a migration that would rename the "jsec_user" table to "users". I'd only
need to change a single line of code when I do that.
Sequel is great for not forcing me to use an ORM all the time and will
make it easy to directly access the underlying database, merging with
the other ORM great features. That is awesome and I don't know any other
libraries that would allow me to do so.
Don't get me wrong, I really know SQL and have no problems on using it.
Actually I always check the generated SQL. But this doesn't mean I don't
find lots of features of ORMs very powerful. But what I'm asking is just
a simple as/alias method that would make my code easier to read and I
don't think this feature would hurt Sequel design in any way.
Also, this would confuse others needing to maintain this
application in the future (currently I'm the only maintainer).
class Page < Sequel::Model(:page)
end
class User < Sequel::Model(:jsec_user)
end
Model.table_name gives the table name, if you don't actually know
it in advance.
Yes, this is what I'm currently using, but see the difference:
LongDomainNameForPage.from(Sequel.as(LongDomainNameForPage.table_name,
:p)).eager_graph(:user).select(:p__name, :jsec_user__name).first
LongDomainNameForPage.as(:p).eager_graph(:user, as:
:u).select(:p__name, :u__name).first
Your use case is very specific, I doubt most people have it.
This is not the first project I have this need and I don't think I'm the
only one with such needs. Actually this is far common to anyone used to
deal with SQL and reports. I needed things like that all the time too in
my old job with different projects.
You are free to do:
class Sequel::Model
def self.as(aliaz)
from(Sequel.as(table_name, aliaz))
end
end
Yes, I know, but if this could be added to Sequel, it would be easier
for others to figure out what this method does. I wouldn't know where
this method comes from if I wasn't the one who is requesting it. I mean,
if I was reading someone else's code, I'd look for "as" in the Sequel
documentation and finally would ask here about it.
It is just that I think this is pretty useful to many people and
wouldn't hurt Sequel anyway.
The eager_graph complaint is moot, as eager_graph uses the association
name as the alias, so even if User.table_name is foo,
eager_graph(:user) is going to alias it to user unless that table name
has already been used.
Yes and that is great, but this is not what I was talking about. What if
the same association name happens more than once in the association
chain? How would it be aliased? (nevermind, answered below - check my
comments)
So, I was thinking that we could handle aliasing in Sequel
models like this:
Page.as(:p).eager_graph(:user, as: :u).select(:p__name,
:u__name).first
eager_graph automatically sets up aliases for you, using a
deterministic method that handles self references, so you should
not need to do this manually.
And how do I know what is the alias for usage in my 'where' clauses?
Like I said, it's deterministic. It'll be the name of the association
unless that has already been used, and if it has, it just appends a
number. You can do full introspection on the dataset's opts if you
want to find out what alias was used.
Exactly, but it is not great for me to depend on Sequel's internal if I
use something like "where(user1__name)". Also, "user1__name" doesn't
make any sense when reading the code. Alias are meant to give some
meaning to that table role in the query.
Best,
Rodrigo.
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sequel-talk?hl=en.