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

Matt S Trout dbix-class at trout.me.uk
Wed Apr 5 14:26:02 CEST 2006


Daniel Westermann-Clark wrote:
> On 2006-04-04 22:26:28 +0200, Goetz Bock wrote:
>> as discussed on irc, I've some problems with unique_contrains beeing
>> "ignored" when using find_or_create.
> 
> Yes, my apologies.  I implemented the unique constraints.  When you
> mentioned this I was getting another project out the door.  (Anybody
> want a Catalyst- and DBIC-based voting system?  I'm trying to get
> $work to open source it, but we'll see.)
> 
>> This is the test case:
>>
>> ------------------------------------------------------------------------
>> 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.
> 
> Thanks for the test case.  This is indeed a bug.  I've worked up an
> initial patch (see attached) to fix this, but looking at it again I
> wonder if find should simply be reorganized to work more like
> update_or_create.

Yes. Yes it should. Your update_or_create implementation makes me embarrassed 
by the find(_*)? implementation.

>> The problem here is there is no update_or_create_related(), at last
>> not in the docs.
> 
> No, it does not exist currently.  I think for now you can do something
> like:
> 
> my $artist = $schema->resultset('Artist')->find(1);
> my $cd = $artist->search_related('cds')->update_or_create(...);

or $artist->cds->update_or_create(...)

which I personally prefer.

> but a wrapper should probably be added.  Someone correct me if I'm
> wrong, as I'm not terribly familiar with the Relationship convenience
> methods.

Yes. I didn't realise you hadn't added it when you did update_or_create

-- 
      Matt S Trout       Offering custom development, consultancy and support
   Technical Director    contracts for Catalyst, DBIx::Class and BAST. Contact
Shadowcat Systems Ltd.  mst (at) shadowcatsystems.co.uk for more information

+ Help us build a better perl ORM: http://dbix-class.shadowcatsystems.co.uk/ +



More information about the Dbix-class mailing list