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.
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
I guess we could define it like:
class Page < Sequel::Model(:page__p)
end
Model.table_name even handles this case, returning just the alias.
But what if you need to do some query where there is a self
reference, like if a page could have a parent page?
You join with an alias:
Page.join(:page___parent, :id=>:parent_id).
join(:page___grand_parent, :id=>:parent_id).
where(:parent__name=>..., :grand_parent__name=>...)
Yes, but I don't think this would work for eager loading. For example,
instead of
Page.eager(:user)
I'd need
Page.join(:jsec_user___user)
It means I have to remember that the user's table is jsec_user, which I
don't want to while reading my code.
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?
Well, this is just an idea. Since I'm starting to use Sequel more
often maybe there is already another way to handle this without
being too verbose but I couldn't find it in the docs.
By the way, I don't like the idea of adding extensions to core
classes, so I stopped using MongoMapper and I'm requiring Sequel
without the core extensions.
Well, since 3.34, you shouldn't need to use the core extensions, every
core extension has a similar method defined on the Sequel module.
I can't seem to find much documentation on how to do certain
things without those query extensions. For example, see the
equivalent codes:
where('email is not null')
where{~ {email: null} }
How to do the above without enabling the core extensions?
The simplest way to do that is:
exclude(:email=>nil)
Thanks, I've forgotten about this 'exclude' again :P (I've seen it
several times but I always forget about it - I guess I'm getting old :( )
That because a shortcut exists for this case (inverting a filter). A
general solution would be:
where(Sequel.~(:email=>nil))
I didn't know about Sequel.~. I thought I'd need "~ Sequel.expr(email:
nil)".
Thanks, I'm starting to learn Sequel now :D
Cheers,
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.