[Dbix-class] unique constrains are ignored when using find_or_create

Goetz Bock bock+dbixc at blacknet.de
Tue Apr 4 22:26:28 CEST 2006



Dear list,

as discussed on irc, I've some problems with unique_contrains beeing
"ignored" when using find_or_create.

(My actual problem is using find_or_create_related, but I was unable 
 to come up with a testcase based on the existing DBICTest classes. see
 below)

This is the test case:

------------------------------------------------------------------------
#!/usr/bin/perl
use lib qw(t/lib);
use DBICTest;
use DBICTest::BasicRels;

my $schema = DBICTest->schema;

my $cd = $schema->resultset('CD')->find_or_create( 
    {
        'artist' => 1, 
        'title'  => "Spoonful of bees", 
        'year'   => 2000,
    },
    {
        'key' => 'artist_title',
    }
);

print "The CD is from " . $cd->year . " should be from 1999.\n";
------------------------------------------------------------------------

IMHO it sould find the existing CD from 1999, like update_or_create does.
(btw it does not fail, as SQLite does not enforce the unique constraint.
 But it failes with my MySQL setup.)

That's a bioled down version. Now for my actual problem.

Assuming Artust.pm would have:

__PACKAGE__->has_many( 'cds' => 'CD', 'artistid' );

than my testcase would look like:

----------------------------------------
my $artist = $schema->resultset('Artist')->find(
    { 'name' => 'Caterwauler McCrae' } );

my $cd = $artist->find_or_create_related('cds', 
    {
        'title' => 'Spoonful of bees',
        'year'  => '2000',
    },
    {
        'key' => 'artist_title',
    }
);
----------------------------------------

The problem here is there is no update_or_create_related(), at last not
in the docs.
(Tests done with DBIx-Class-0.06000.)
-- 
/"\ Goetz Bock at blacknet dot de  --  secure mobile Linux everNETting
\ /       (c) 2006 Creative Commons, Attribution-ShareAlike 2.0 de
 X   [ 1. Use descriptive subjects - 2. Edit a reply for brevity -  ]
/ \  [ 3. Reply to the list - 4. Read the archive *before* you post ]



More information about the Dbix-class mailing list