Skip Menu |
 

This queue is for tickets about the DBIx-Class CPAN distribution.

Report information
The Basics
Id: 17387
Status: rejected
Priority: 0/
Queue: DBIx-Class

People
Owner: Nobody in particular
Requestors: gfurger [...] bytecode.ch
Cc:
AdminCc:

Bug Information
Severity: Important
Broken in: 0.04001
Fixed in: (no value)



Subject: copy and auto increment primary key issue and suggested solution
Download (untitled) / with headers
text/plain 883b
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
Download (untitled) / with headers
text/plain 1.2k
On Tue Jan 31 04:15:29 2006, guest wrote: Show quoted text
> I use a Postgres database and a table with an auto incremented primary
key. Show quoted text
> > 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.
There's already been some debate about this; the problem comes if you have a multi-column primary key and only wish to alter one of those during a copy. If you pass 'undef' for the auto-inc PK things should work fine. If you want to re-open the discussion on this, please do so on the mailing list so the rest of the contributors and community can join in.


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

Please report any issues with rt.cpan.org to rt-cpan-admin@bestpractical.com.