1

I have been trying to update a document using laravel ORM but getting exception stating a unknown column updated_at.

This column is not in database table, and neither have I mentioned in the query.

$foo = ProductMatchUnmatchHistory::where('product_match_unmatches_id', $pmu->id)
                    ->update(['is_latest'=>0]);

ProductMatchUnmatchHistory model:

protected $fillable = [
    'product_match_unmatches_id',
    'human_verdict',
    'ai_result',
    'updated_on',
    'updated_by',
    'is_latest'
];

The table has the same columns as in $fillable array, with additional id primary key.

Here's the exception message:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'updated_at' in 'field list' (SQL: update `product_match_unmatch_histories` set `is_latest` = 0, `updated_at` = 2019-11-20 12:20:02 where `product_match_unmatches_id` = 1759)

I have no clue from where the updated_at column is added to the query.

miken32
  • 42,008
  • 16
  • 111
  • 154
Azima
  • 3,835
  • 15
  • 49
  • 95

4 Answers4

4

If you don't want to use the updated_at and created_at fields from Laravel's Eloquent models, edit your ProductMatchUnmatchHistory.php model and set $timestamps = false.

class ProductMatchUnmatchHistory extends Model
{
    //....
    $timestamps = false;
    //....
}

Additional Info

Laravel Models automatically set a created_at field when an entry is created and update the updated_at column when you do any changes to a row. This is handled through the $timestamps field on the model. If set to true, which is default, laravel will assume that your models have created_at and updated_at fields.

In most cases this is pretty helpful. You can easily add these fields in your migration by just adding $table->timestamps();.

See https://laravel.com/docs/5.8/eloquent#eloquent-model-conventions (under Timestamps)

Community
  • 1
  • 1
Matthias S
  • 3,358
  • 3
  • 21
  • 31
1

please change

you must define table name

protected $table = 'your table name';

protected $fillable = [
    'product_match_unmatches_id',
    'human_verdict',
    'ai_result',
    'updated_on',
    'updated_by',
    'is_latest'
];
$timestamps = false;
VIKAS KATARIYA
  • 5,867
  • 3
  • 17
  • 34
0

First please check in your table updated_at exist? because sql error say column not found

0

@Azima you should added the column in your table otherwise you will get this error, because eloquent updating that column and its not present in table or you can $timestamps=false in your Model