我有一个laravel 5项目,我想使用软删除方法,但每次删除后我收到此sql错误消息:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '7' for key 'PRIMARY' (SQL: insert into `fairkatert_task` (`id`, `user_id`, `editor_id`, `title`, `task`, `status`, `deadline_date`, `deleted_at`, `created_at`, `updated_at`, `milestone_id`) values (7, 1, 1, asdasdasdasdasd, asdasdasdasdasdasdasda, open, 2016-12-28 00:00:00, 2015-10-02 07:16:26, 0000-00-00 00:00:00, 0000-00-00 00:00:00, 0))
我试图关闭时间戳,但它没有停止,我谷歌它,我找不到任何解决方案。
迁移:
public function up()
{
Schema::create('task', function(Blueprint $table)
{
$table->increments('id');
$table->string('user_id')->references('id')->on('user')->onDelete('cascade');
$table->integer('editor_id')->references('id')->on('user')->onDelete('cascade');
$table->string('title');
$table->text('task');
$table->enum('status', array('open', 'close', 'reopen'))->default('open');
$table->timestamp('deadline_date');
$table->softDeletes();
$table->timestamps();
});
}
模型:
<?php
namespace App\Http\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Task extends Model
{
use SoftDeletes;
public $timestamps = true;
protected $table = 'task';
protected $fillable = [ 'user_id', 'editor_id', 'title', 'task', 'status', 'deadline_date' ];
protected $dates = [ 'deadline_date', 'deleted_at', 'created_at', 'updated_at' ];
谢谢你的帮助。
答案 0 :(得分:1)
谢谢你,我发现了我的问题。
$task->delete();
$task->save();
我不知道为什么,但我在删除记录后使用了save方法。 这很容易,问题解决了。