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.
 

> 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.  You are free 
to do:

  class Sequel::Model
     def self.as(aliaz)
       from(Sequel.as(table_name, aliaz))
     end
  end
 
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.

 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. 

Jeremy

>

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/sequel-talk/-/dlVdDDfg37oJ.
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.

Reply via email to