1 package DBIx::Class::PK::Auto;
3 #use base qw/DBIx::Class::PK/;
4 use base qw/DBIx::Class/;
10 DBIx::Class::PK::Auto - Automatic primary key class
14 __PACKAGE__->load_components(qw/PK::Auto Core/);
15 __PACKAGE__->set_primary_key('id');
19 This class overrides the insert method to get automatically incremented primary
22 __PACKAGE__->load_components(qw/PK::Auto Core/);
24 Note that C<PK::Auto> is specified as the left of the Core component.
25 See L<DBIx::Class::Manual::Component> for details of component interactions.
29 C<PK::Auto> does this by letting the database assign the primary key field and
30 fetching the assigned value afterwards.
36 Overrides C<insert> so that it will get the value of autoincremented primary
42 my ($self, @rest) = @_;
43 my $ret = $self->next::method(@rest);
45 my ($pri, $too_many) = grep { !defined $self->get_column($_) ||
46 ref($self->get_column($_)) eq 'SCALAR'} $self->primary_columns;
47 return $ret unless defined $pri; # if all primaries are already populated, skip auto-inc
48 $self->throw_exception( "More than one possible key found for auto-inc on ".ref $self )
51 my $storage = $self->result_source->storage;
52 $self->throw_exception( "Missing primary key but Storage doesn't support last_insert_id" )
53 unless $storage->can('last_insert_id');
54 my $id = $storage->last_insert_id($self->result_source,$pri);
55 $self->throw_exception( "Can't get last insert id" ) unless $id;
56 $self->store_column($pri => $id);
63 Manually define the correct sequence for your table, to avoid the overhead
64 associated with looking up the sequence automatically.
70 foreach my $pri ($self->primary_columns) {
71 $self->column_info($pri)->{sequence} = $seq;
79 Matt S. Trout <mst@shadowcatsystems.co.uk>
83 You may distribute this code under the same terms as Perl itself.