[Dbix-class] Duplicate entry error on create

Eden Cardim edencardim at gmail.com
Wed Dec 9 17:10:39 GMT 2009


>>>>> "Dermot" == Dermot  <paikkos at googlemail.com> writes:

    Dermot> Hi, I am getting the following error when I attempt to
    Dermot> create a record.

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

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

No, that's a mysql error, you're probably violating a constraint on your
table with those values.
 
    Dermot> should I be doing
    Dermot> 
    Dermot> my $contrib = $schema->resultset('Contributors')->find_or_create(
    Dermot>     {
    Dermot>         code => $var,
    Dermot>         name => $name,
    Dermot>     },
    Dermot>     { key => code_UNIQUE }
    Dermot> );
    Dermot> my $row = $schema->resultset('Products')->find_or_create(
    Dermot>     {
    Dermot>         distribution   => 1,
    Dermot>         caption        => 'Earth',
    Dermot>         contributor_id => $contrib
    Dermot>     }
    Dermot> );
    Dermot> Am I expecting too much for find_and_create to
    Dermot> automagically_vivicationally cascade into related tables?

No, you're just doing it wrong, try:

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

-- 
   Eden Cardim       Need help with your Catalyst or DBIx::Class project?
  Code Monkey                    http://www.shadowcat.co.uk/catalyst/
 Shadowcat Systems Ltd.  Want a managed development or deployment platform?
http://edenc.vox.com/            http://www.shadowcat.co.uk/servers/




More information about the DBIx-Class mailing list