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 # In your table classes (replace PK::Auto::SQLite with your
16 __PACKAGE__->load_components(qw/PK::Auto::SQLite Core/);
17 __PACKAGE__->set_primary_key('id');
21 This class overrides the insert method to get automatically
22 incremented primary keys.
24 You don't want to be using this directly - instead load the
25 appropriate one for your database, e.g. C<PK::Auto::SQLite>, in your
28 __PACKAGE__->load_components(qw/PK::Auto::SQLite Core/);
30 Note that C<PK::Auto::SQLite> is specified as the leftmost argument.
32 Alternatively, you can load the components separately:
34 __PACKAGE__->load_components(qw/Core/);
35 __PACKAGE__->load_components(qw/PK::Auto::SQLite/);
37 This can be used, for example, if you have different databases and
38 need to determine the appropriate C<PK::Auto> class at runtime.
42 C<PK::Auto> does this by letting the database assign the primary key
43 field and fetching the assigned value afterwards.
49 Overrides insert so that it will get the value of autoincremented
55 my ($self, @rest) = @_;
56 my $ret = $self->next::method(@rest);
58 # if all primaries are already populated, skip auto-inc
60 map { $populated++ if defined $self->get_column($_) } $self->primary_columns;
61 return $ret if ( $populated == scalar $self->primary_columns );
63 my ($pri, $too_many) =
64 (grep { $self->column_info($_)->{'auto_increment'} }
65 $self->primary_columns)
66 || $self->primary_columns;
67 $self->throw( "More than one possible key found for auto-inc on ".ref $self )
69 unless (defined $self->get_column($pri)) {
70 $self->throw( "Can't auto-inc for $pri on ".ref $self.": no _last_insert_id method" )
71 unless $self->can('last_insert_id');
72 my $id = $self->last_insert_id;
73 $self->throw( "Can't get last insert id" ) unless $id;
74 $self->store_column($pri => $id);
81 Manually define the correct sequence for your table, to avoid the overhead
82 associated with looking up the sequence automatically.
86 __PACKAGE__->mk_classdata('sequence');
92 Matt S. Trout <mst@shadowcatsystems.co.uk>
96 You may distribute this code under the same terms as Perl itself.