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
16 This class overrides the insert method to get automatically
17 incremented primary keys.
19 You don't want to be using this directly - instead load the appropriate
20 one for your database, e.g. PK::Auto::SQLite
24 PK::Auto does this by letting the database assign the primary key field
25 and fetching the assigned value afterwards.
31 Overrides insert so that it will get the value of autoincremented
37 my ($self, @rest) = @_;
38 my $ret = $self->next::method(@rest);
40 # if all primaries are already populated, skip auto-inc
42 map { $populated++ if defined $self->get_column($_) } $self->primary_columns;
43 return $ret if ( $populated == scalar $self->primary_columns );
45 my ($pri, $too_many) =
46 (grep { $self->column_info($_)->{'auto_increment'} }
47 $self->primary_columns)
48 || $self->primary_columns;
49 $self->throw( "More than one possible key found for auto-inc on ".ref $self )
51 unless (defined $self->get_column($pri)) {
52 $self->throw( "Can't auto-inc for $pri on ".ref $self.": no _last_insert_id method" )
53 unless $self->can('last_insert_id');
54 my $id = $self->last_insert_id;
55 $self->throw( "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.
68 __PACKAGE__->mk_classdata('sequence');
74 Matt S. Trout <mst@shadowcatsystems.co.uk>
78 You may distribute this code under the same terms as Perl itself.