jeudi 15 novembre 2018

Can't modify 'updated_at' in my database after adding a column with a migration in Laravel 5.1

I am trying to add a new integer "id" column in a table of my database to map every row with an id. In order to do so, I am using a migration in Laravel 5.1. The run() function of the migration is exactly the following:

public function up()
{
    Schema::table('license_keys', function($table) {
        $table->integer('id_nuevo');
    });
}

The table I am trying to modify is set with default 'updated_at' and 'created_at' timestamps.

I execute the migration and the column is added correctly. I made sure to add the new column in the $fillable variable in my model. The next step in the process is to set the ids correctly, because that column is created with all 0s. In order to do so, I am using a Seeder with the following code:

public function run()
{
    $i=1;
    $licenses = LicenseKey::getEveryLicenseKey();
    foreach ($licenses as $license){
        $license->id_nuevo = $i;
        //$license->timestamps = false;
        $license->save();
        $i++;
    }

}

But the problem starts here. When I try to update any field of any row with the save() function it gives me the following error:

[Illuminate\Database\QueryException]                                                                                                                                                                     
 SQLSTATE[21S01]: Insert value list does not match column list: 1136       Column count doesn't match value count at row 1 (SQL: update `license_keys`   set `id_nuevo` = 1, `updated_at` = 2018-11-15 13:24:11   
 where `hash_key` = 0...0b)                                                                                                                                                     



  [PDOException]                                                                                                       
  SQLSTATE[21S01]: Insert value list does not match column list: 1136 Column count doesn't match value count at row 1

But if I set the timestamps to false (commented line in the code), the operation succeeds. Even if I try to manually change the value of the 'updated_at' column in phpMyadmin, it gives me the same error. Can anybody help me with this problem?

Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire