[Dbix-class] Auto-generate random primary keys

Matt S Trout dbix-classtrout.me.uk at central.redice.net
Wed Mar 1 14:22:14 CET 2006


On Wed, Mar 01, 2006 at 11:58:23AM +0100, Bernhard Graf wrote:
> Matt S Trout wrote:
> > On Tue, Feb 28, 2006 at 03:55:08PM +0100, Bernhard Graf wrote:
> > > Hi,
> > >
> > > I just started playing around with DBIC.
> > >
> > > Some tables in my db have random primary keys created by eg.
> > > Digest::MD5::md5_hex($$, rand, time).
> > >
> > > Is there a recommended way to do this?
> > >
> > > I thought of a DBIC::PK::Auto::MD5 class, with an insert() that
> > > does basically:
> > >
> > > my $self = shift;
> > > $self->store_column(md5_hex($$, rand, time));
> > > $self->next::method(@_);
> > >
> > > Is that an appropriate approach?
> >
> > http://search.cpan.org/~mikihoshi/DBIx-Class-RandomStringColumns-0.02
> >/lib/DBIx/Class/RandomStringColumns.pm
> 
> Now I tried it an it fails.
> 
> I went the schema way.
> 
> This is the error message:
> 
> Can't locate object method "search" via package "My::Schema::User" at /usr/lib/perl5/vendor_perl/5.8.5/DBIx/Class/RandomStringColumns.pm line 80.
> 
> Look at
> http://search.cpan.org/src/MIKIHOSHI/DBIx-Class-RandomStringColumns-0.02/lib/DBIx/Class/RandomStringColumns.pm
> 
> do { # must be unique
>     $val = String::Random->new->randregex(sprintf('%s{%d}', $self->rs_salt->{$column} , $self->rs_length->{$column}));
> } while ($self->search({$column => $val}));
> 
> $self is a My::Schema::User object that obviously has no search method.
> If I read the docs I see that search is in Class DBIx::Class::ResultSet.
> Does my My::Schema::User should inherit from DBIx::Class::ResultSet?
> Or is DBIx::Class::RandomStringColumns broken?

It's broken.

} while ($self->search({$column => $val}));

should be

} while ($self->result_source->resultset->search({$column => $val}));

-- 
     Matt S Trout       Offering custom development, consultancy and support
  Technical Director    contracts for Catalyst, DBIx::Class and BAST. Contact
Shadowcat Systems Ltd.  mst (at) shadowcatsystems.co.uk for more information

 + Help us build a better perl ORM: http://dbix-class.shadowcatsystems.co.uk/ +



More information about the Dbix-class mailing list