Eloquent update () failed due to ambiguity of updated_at table

Well, this question is related to installing Laravel 4.1.23. I am trying to update multiple records using the Eloquent update () method in a query that includes a connection:

ChildSchoolYear::whereNull('exit_date')-> join('school_years', 'child_school_years.school_year_id','=','school_years.id')-> update(array('child_school_years.exit_date'=>'`school_years`.`end_date`', 'child_school_years.editor_id'=>$userId)) 

Laravel generates the correct SQL for the contents of the query, which I provide above, but the generated full SQL statement

 update `child_school_years` inner join `school_years` on `child_school_years`.`school_year_id` = `school_years`.`id` set `child_school_years`.`exit_date` = `school_years`.`end_date`, `child_school_years`.`editor_id` = 2, `updated_at` = 2014-08-15 02:00:33 where `exit_date` is null) 

This will work, except that the automatically added updated_at field exists in the child_school_years and school_years tables, so adding a Laravel field throws an Integrity constraint violation: 1052 Column 'updated_at' in field list is ambiguous exception Integrity constraint violation: 1052 Column 'updated_at' in field list is ambiguous .

Any suggestions on how to domesticate the updated part? I would be happy if the field was updated, but I will live without it, if necessary, if I can eliminate it.

+6
source share
1 answer

It's impossible to change the behavior of Eloquent, even adjusting the UPDATED_AT column UPDATED_AT not help, so you need to use either a simple Query\Builder , as already suggested, or one of the methods below that I find a bit better:

 // easiest way ChildSchoolYear::whereNull('exit_date') ->join('school_years', 'child_school_years.school_year_id','=','school_years.id') ->getQuery() // get underlying base Query\Builder ->update( array( 'child_school_years.exit_date' => '`school_years`.`end_date`', 'child_school_years.editor_id' => $userId, 'child_school_years.updated_at' => Carbon\Carbon::now(), ) ); // also would work, temporary turn off auto timestamps with($model = new ChildSchoolYear)->timestamps = false; // above is the same as: // $model = new ChildSchoolYear; // $model->timestamps = false; $model->whereNull('exit_date') ->join('school_years', 'child_school_years.school_year_id','=','school_years.id') ->update( array( 'child_school_years.exit_date' => '`school_years`.`end_date`', 'child_school_years.editor_id' => $userId, 'child_school_years.updated_at' => Carbon\Carbon::now(), ) ); 
+11
source

Source: https://habr.com/ru/post/973849/


All Articles