Subject: | copy and auto increment primary key issue and suggested solution |
I use a Postgres database and a table with an auto incremented primary key.
I need a copy of a saved row and want to use the DBIx::Class::Row->copy
method to do that.
But, DBIx::Class::PK::Auto report an errror because copy try to insert
this copy with the same primary key.
I suggest in DBIx::Class::Row->copy to delete the primary keys in the
new instance before inserting attribut changes like this:
sub copy {
my ($self, $changes) = @_;
my $new = bless({ _column_data => { %{$self->{_column_data}}} }, ref
$self);
+ delete $new->{_column_data}->{$_} for $self->primary_columns;
$new->set_column($_ => $changes->{$_}) for keys %$changes;
return $new->insert;
}
This will permit auto increment to corretly work (via the overriden
insert method) and permits to manualy modifying primary keys if needed
via copy method arguments.
Thanks for your great work!
Gilbert