3

I'm using Laravel Eloquent's firstOrNew() function to retrieve a DB record based on 3 criteria:

$summary = $this->firstOrNew(array(
    'date' => $date,
    'product_id' => $product_id,
    'store_id' => $store_id,
     ));

I also have a unique composite key of those 3 fields.

Even though the record exists, the function doesn't retrieve it, so when I update an attribute and run $summary->save() I get the wonderful

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry >'2015-01-02-6-23' for key 'date_product_store_unique'

Any ideas?

Laravel version is 4.2.

Shannon
  • 33
  • 1
  • Are you sure the data store in the variables that are passed is what you are expecting? – Bogdan Mar 05 '15 at 22:53
  • I've debugged the input, in fact, the firstOrNew() function creates a new object with that exact data. I believe the issue has to do with the unique key not being the primary key, but I'm not sure. – Shannon Mar 06 '15 at 00:46
  • Laravel 4 doesn't support composite keys http://laravel.io/forum/09-25-2014-models-for-tables-with-multiple-columns-for-primary-key maybe Laravel 5 does... I dont know – cawecoy Mar 23 '15 at 05:10

1 Answers1

2

Laravel 4.2 doesnt support composite keys!

Ajay Kumar Ganesh
  • 1,838
  • 2
  • 25
  • 33