On Oct 20, 7:59 pm, Gavin Kistner <[email protected]> wrote:
> Making a guess:
>
> Model.[] allows a single integer because there is a one-to-one  
> correspondence with a DB table and the primary key or keys are known.
>
> A dataset may represent a join or other composition of multiple  
> tables, or a subset of columns from a single table (possibly without  
> any primary key column(s)). Thus, Dataset#[] can't reliably know  
> anything about the primary key column(s) that may or may not be present.
>
> Am I right?

Pretty close.  For regular datasets, there is no knowledge of any
primary keys, so it can't lookup by primary key

We could theoretically override it for model datasets to work similar
to Model.[].  However, it would be a bit problematic because of the
way things are set up.  For example, if it worked the same way as
Model.[], it would work fine for one_to_many association datasets, but
not for many_to_many association datasets where the join table has a
column with the same column name as a name in the join table.  This is
because the lookup uses an unqualifed primary key.  Even if we use a
qualified primary key lookup, it would fail in some cases in ways not
really possible to fix:

  # Artist.many_to_many :albums
  # join table and albums table both have :id columns
  artist = Artist[1]

  # Will fail for unqualified primary key
  artist.albums_dataset[1]

  # Will fail for qualified primary key
  artist.albums_dataset.from_self[1]

I think it's best that we don't do this, and instead force the user to
be explicit and use what is appropriate for their particular case.
That's the current behavior, and I think it makes sense.

One of the great things about Sequel is that it is easy to add this
yourself if you want it:

  class Sequel::Model
    def_dataset_method(:[]){|*v| (v.length == 1 && v[0].is_a?
(Integer)) ? super(model.primary_key_hash(v[0])) : super(*v)}
  end

Jeremy
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to