[Dbix-class] A more accurate redux of zby's comments

Zbigniew Lukasiak zzbbyy at gmail.com
Fri Jan 25 09:10:45 GMT 2008


On Jan 25, 2008 8:07 AM, Matt S Trout <dbix-class at trout.me.uk> wrote:
> >  what would that
> > 'proceed as normal' mean? Would it mean that the uniqueness is not
> > checked at all, the query is reduced by all columns that don't belong
> > to any of the unique constraints (or that don't belong to the chosen
> > unique constraint), then it is run and the first row is returned?
>
> It would mean that DBIC would accept the user saying "I know you can't tell
> but this is unique". Possibly we should also check to see if the sth is
> exhausted after getting the single row and produce a warning or exception
> if not.

Great!  That is what I believe is needed.

> >
> > When you do know the PK then you would call find, when you do know
> > that you have no PK then you would directly call create or new.  It is
> > only when you don't know that you need any of these listed methods.
>
> I think you're (a) assuming PK rather than one of N unique keys, (b)
> assuming an auto-increment or other surrogate PK rather than a natural one.
>
> Much of the utility of the *_or_* methods is when one or both of these is
> not the case.

It's hard to analyse that without use cases.  Here is the one that I
was mostly thinking about:

Code to edit/create a record from web form parameters.  You receive a
bunch of columns and you need to update the record identified by the
columns that form the PK or create a new one if there is no such
column (including the case when the PK parameters are set to 'undef'
so that they would receive the default value from the DB).   It would
be very useful to have a method on the resultset that would
automatically analyse the parameters and do the right thing.
update_or_create is unfortunately unsuitable for that - because you
cannot delete the PK from the list of parameters even if it is undef
(otherwise find will not always work as expected) and you cannot leave
it there if it is undef - beause then create will fail.

And just to explain myself - I really try to contribute to the project
- and I hope that this documenting of the gotchas that an
unexperienced DBIC programmer can encounter when using it is a
contribution and not an evil attack.

Cheers,
Zbigniew
http://perlalchemy.blogspot.com/



More information about the DBIx-Class mailing list