inblosam has asked for the wisdom of the Perl Monks concerning the following question:

I am using Class::DBI for working with my mysql database. I have a program that gets listings of products from a web service, and I am trying to keep a copy of some of the aspects of the product. When I use "find_or_create" to create a record unless one already exists, it will instead add the products despite an exact copy already exists. I have the find_or_create call in a for loop that makes a hash every cycle and passes that to the database with find_or_create. 16 out of 21 times it duplicates an existing entry (that is my test data for now). I have compared the hash being sent with Data::Dumper and it is exact in all cases. I am wondering if something else is interfering. Any ideas would be helped!
  • Comment on Class::DBI find_or_create creating when shouldn't

Replies are listed 'Best First'.
Re: Class::DBI find_or_create creating when shouldn't
by PodMaster (Abbot) on Jun 01, 2004 at 06:19 UTC
    When I use "find_or_create" to create a record unless one already exists, it will instead add the products despite an exact copy already exists.
    The problem may be a bug in Class::DBI, it may be mysql specific ... it's pointless to speculate. Please post some code which demonstrates the problem, so as we can rule out user error and narrow down where the problem occurs.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      Below is some of the code. The Class::DBI part works perfect everywhere else I am using it. I tried passing in the parameters directly in the find_or_create, and with the hash. I also tried not deferencing the hash in the find_or_create().
      my %list_params = ( listnum => $listnum, address => $address, city => $city, state => $state, zip => $zip, tot_bed => $tot_bed, tot_bath => $tot_bath, tot_sqft => $tot_sqft, home_style => $home_style, home_type => $home_type, sch_element => $sch_element, sch_intermed=> $sch_intermed, sch_highsch => $sch_highsch, price => $price, photo => $photos[0][2], acres => $acres ); print Dumper(\%list_params); $addlisting = Table::Listings->find_or_create(\%list_params);
      Both were added to the database for the following hashes, when in reality only the first should have:
      $VAR1 = { 'sch_element' => 'Salina', 'home_style' => 'Manufact/Modular', 'photo' => 'http://photo.somesite.com/280x210/419964.jpg', 'tot_bed' => '3', 'acres' => '0.270000', 'tot_bath' => '2', 'state' => 'UT', 'home_type' => 'Single Family', 'zip' => '84614', 'city' => 'Salina', 'listnum' => '419764', 'price' => '88900', 'sch_highsch' => 'North Sevier', 'address' => ' SALINA CREEK DR', 'sch_intermed' => 'Red Hills', 'tot_sqft' => '1404' }; $VAR1 = { 'sch_element' => 'Salina', 'home_style' => 'Manufact/Modular', 'photo' => 'http://photo.somesite.com/280x210/419964.jpg', 'tot_bed' => '3', 'acres' => '0.270000', 'tot_bath' => '2', 'state' => 'UT', 'home_type' => 'Single Family', 'zip' => '84614', 'city' => 'Salina', 'listnum' => '419764', 'price' => '88900', 'sch_highsch' => 'North Sevier', 'address' => ' SALINA CREEK DR', 'sch_intermed' => 'Red Hills', 'tot_sqft' => '1404' };
      In the following find_or_create the first attempt was added (blank table) and then the second one was not added. So this is how it is supposed to work, but again only 5 of 21 work properly.
      $VAR1 = { 'sch_element' => 'Monroe', 'home_style' => 'Rambler/Ranch', 'photo' => 'http://photo.somesite.com/280x210/415584.jpg', 'tot_bed' => '5', 'acres' => '3.000000', 'tot_bath' => '1', 'state' => 'UT', 'home_type' => 'Single Family', 'zip' => '84777', 'city' => 'Annabella', 'listnum' => '415684', 'price' => '63900', 'sch_highsch' => 'South Sevier', 'address' => ' N ANNABELLA RD ', 'sch_intermed' => 'South Sevier', 'tot_sqft' => '2392' }; $VAR1 = { 'sch_element' => 'Monroe', 'home_style' => 'Rambler/Ranch', 'photo' => 'http://photo.somesite.com/280x210/415584.jpg', 'tot_bed' => '5', 'acres' => '3.000000', 'tot_bath' => '1', 'state' => 'UT', 'home_type' => 'Single Family', 'zip' => '84777', 'city' => 'Annabella', 'listnum' => '415684', 'price' => '63900', 'sch_highsch' => 'South Sevier', 'address' => ' N ANNABELLA RD ', 'sch_intermed' => 'South Sevier', 'tot_sqft' => '2392' };
      Also if there is a / in the text it gets cut off after that, so it will say Manufact/ instead of Manufact/Modular in the field, or http:// instead of the full URL. Any ideas on that one too? I guess the other thing to do is just check the database if it comes up with a match and then if it doesn't just create the file like normal. Anyone come across similar problems? -Michael Jensen
        I've never had problems like the ones you are talking about here. I'd suggest you turn on DBI_TRACE and look at the SQL that is being sent to the database.