【发布时间】:2016-07-09 11:54:16
【问题描述】:
我是 laravel 5.2 和 postgres pgsql 的新手。
我有一张名为logs的表
Schema::create('logs', function(Blueprint $table){
$table->bigIncrements('id');
$table->text('exception');
$table->dateTime('created_at');
});
和相应的模型Log
class Log extends Model{
public $incrementing = true;
public $timestamps = false;
}
据我了解(可能有误)。如果没有给出id,那么 Eloquent 会将最后使用的 id + 1(但不是 null 反对它)。
如果我先保存Log,那么它的 id 肯定是 1,下一个是 2。
假设我只添加了一条 Eloquent save() 和 id=1 的记录
但是问题来了,如果我以某种方式(手动使用插入查询或使用 csv 文件)添加 ID 为 2,3,5,8 等的新记录。在表中。但以编程方式在save 上,
Eloquent 仍然将id = 2 设置为第二个Log 记录/实体。
我现在这样保存
$l = new Log();
$l->exception = "some exception";
$l->created_at = Carbon::now()->toDateTimeString();
while(true){
try{
$l->save(); //it auto increments the id on each try
break;
}catch(\Exception $e){
continue;
}
}
观看循环,上面的解决方案对我有用,它会自动添加带有id = 4 的记录(在上述条件下)。它抛出并捕获 id 2 和 3 的两个异常。
我不喜欢这个解决方案,请给我一些建议,我正在寻找一个没有循环的修复,将近 2 天。提前致谢。
【问题讨论】:
标签: php postgresql laravel eloquent plpgsql