Supreme Dolphin
Supreme Dolphin

Reputation: 2594

Laravel update sometimes uses 'INSERT' SQL

I am experiencing some weird issue with updating records in Laravel. I use this code to update a record:

$student = new Student();
$student->exists = true;
$student->reg_number = $request->post('reg_number');
$student->name = $request->post('name');
$student->level_id = $request->post('level_id');
$student->status = $request->post('status'); 
$student->save();

The code executes fine for the first time. However, when I try to update the same record again, I get the error

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'F17/2019/2019' for key 'PRIMARY' (SQL: insert into students (reg_number, name, level_id, updated_at, created_at) values (F17/2019/2019, John Does, 5, 2019-08-08 10:31:52, 2019-08-08 10:31:52)).

However, after sometime, say half an hour or so, I can update the record again without the error, but only once. A second try brings back the error.

I have tried various other techniques discussed in this question, but the results are identical. The error suggests that Laravel is opting to use INSERT instead of UPDATE SQL statement, whereas the record exists and I have explicitly specified that it should update an existing record.

Does anyone know what's going on here?


I am usng MySQL, my students table PRIMARY KEY is reg_number of type VARCHAR. The student controller looks like so:

namespace App;
use Illuminate\Database\Eloquent\Model; 
class Student extends Model
{
    protected $fillable = [
        'reg_number', 
        'name', 
        'level_id', 
        'status' 
    ];
    protected $primaryKey = 'reg_number';
    public $incrementing = false;
}

Upvotes: 0

Views: 1778

Answers (3)

Supreme Dolphin
Supreme Dolphin

Reputation: 2594

I had an error in some other part of the code that I hadn't posted. I have the function

public function addStudent(Request $request) {
    if($request->post('status') !== null){
        $this->updateStudent($request);
        return false;
    }
...

and this is where the error was arising from as I was checking $request->post('status') which was sometimes null when updating. Please vote the question to be closed as the error is not arising from the part of the code I have posted.

I was banging my head because Laravel is sometimes ambiguous with error reporting. If it stated the line number or the function that was causing the error, I would have debugged it much easily and wouldn't have ended up posting an irrelevant question.

Upvotes: 0

Nana Attractive
Nana Attractive

Reputation: 11

try to change value from false to true

public $incrementing = true;

Upvotes: 0

Sagar Gautam
Sagar Gautam

Reputation: 9389

You have to find the student you want to update. Because New will always create new instance of model and when you save, it will create new record instead of update.

$student = Student::find($id);
$student->exists = true;
$student->reg_number = $request->post('reg_number');
$student->name = $request->post('name');
$student->level_id = $request->post('level_id');
$student->status = $request->post('status'); 
$student->save();

Another way to update record is:

$data = [
   // Column values you want to update
   'exists' => 'value' 
    ... 
    'status' => 'value'
];

Student::where('id', $id)->update($data)

Upvotes: 3

Related Questions