PHP Version 5.6.14-1+deb.sury.org~trusty+1
Laravel version: 5.1.27
Table without primary keys (postgresql):
CREATE TABLE "public"."demo" (
"marked_at" timestamptz(6),
"foo" int4
)
WITH (OIDS=FALSE)
;
INSERT INTO "public"."demo" VALUES ('2015-12-22 09:20:27+00', '3');
Model:
<?php
use Illuminate\Database\Eloquent\Model;
class Demo extends Model
{
protected $guarded = [];
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'demo';
/**
* @var array
*/
public $timestamps = false;
/**
* The primary key for the model.
*
* @var string
*/
protected $primaryKey = null;
/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = false;
}
Sample code:
Demo::updateOrCreate(['marked_at' => '2015-12-22 09:20:27+00'], ['foo' => mt_rand(0, 10000)]);
Basically, updating existing row crashes a server. In apache error log I can see:
PHP message: PHP Fatal error: Maximum function nesting level of '256' reached, aborting!
Increasing this value (I mean nesting level) does not help. This error is related to xdebug. Adding primary key id is the solution in this case but in this situation, updateOrCreate shouldn't update row by marked_at field?
Not sure if this is actually meant to be supported by us. Ping @taylorotwell.
@adam-boduch Eloquent always requires primary key for updating. Even normal update won't work without it.
In your case you can simply make marked_at your key and it will work.
@acasar I was pulling out my hair for hours over this, so definitely relieved to find this page. I was curious, when should we set primarykey to null then?
I thought this would be the exact use case for that?
@concept47 It's actually quite simple - if you need to update your model at any point or you want to use Eloquent relations, you'll need to define a primary key. In other cases feel free to set it to null.
That's because Eloquent needs to construct a where condition to update the record in the database and there is no way to do that without the primary key.