[Dbix-class] Duplicate entry error on create

Dermot paikkos at googlemail.com
Wed Dec 9 14:56:52 GMT 2009


Hi,

I am getting the following error when I attempt to create a record.

DBD::mysql::st execute failed: Duplicate entry '0' for key 3 [for
Statement "INSERT INTO products ( distribution, caption,
contributor_id, ...) VALUES(( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )" with
ParamValues: 0='1', 1='Earth', 2='3'....)

Is 'key 3' referring to 'the 3rd item in the INSERT list (contributor_id)?

contributor_id is a foreign key. There is a one_to_many between
products and the Contributors class/table.

__PACKAGE__->belongs_to(
  "contributor_id",
  "MyAPP::Schema::Contributors",
  { contributor_id => "contributor_id" },
);


Assuming the error is referring to belongs_to, is it complaining
because there is an existing record in that table? Or more explicitly,
instead of this:

my $row = $schema->resultset('Products')->find_or_create({
                                    distribution      => 1,
                                    caption            => 'Earth',
                                    contributor_id  => {code=>$var,
name=>$name},
                                 });

should I be doing


my $contrib = $schema->resultset('Contributors')->find_or_create({
                                code => $var,
                                name => $name,
                             },
                             { key => code_UNIQUE});
my $row = $schema->resultset('Products')->find_or_create({
                                    distribution      => 1,
                                    caption            => 'Earth',
                                    contributor_id  =>$contrib
                                 });



Am I expecting too much for find_and_create to
automagically_vivicationally cascade into related tables?
TIA,
Dp.



More information about the DBIx-Class mailing list