在Laravel 5.1中添加带有迁移的列后,无法修改数据库中的“ updated_at”

时间:2018-11-15 12:54:59

标签: php laravel-5.1

我正在尝试在数据库表中添加一个新的整数“ id”列,以映射具有id的每一行。为此,我在Laravel 5.1中使用了迁移。迁移的run()函数如下所示:

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

我要修改的表是使用默认的'updated_at'和'created_at'时间戳设置的。

我执行迁移,并且列已正确添加。我确保在模型的$ fillable变量中添加新列。该过程的下一步是正确设置ID,因为该列创建时全为0。为此,我使用带有以下代码的Seeder:

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

}

但是问题从这里开始。当我尝试使用save()函数更新任何行的任何字段时,都会出现以下错误:

[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

但是,如果我将时间戳设置为false(代码中的注释行),则操作成功。即使我尝试手动更改phpMyadmin中'updated_at'列的值,它也会给我同样的错误。有人可以帮我解决这个问题吗?

表的结构为:

CREATE TABLE `license_keys` (
`hash_key` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`user_id` int(10) UNSIGNED NOT NULL,
`plan_id` int(10) UNSIGNED NOT NULL,
`id_nuevo` int(11) NOT NULL,
`max_credits` bigint(20) NOT NULL,
`max_models` int(11) NOT NULL,
`max_model_categories` int(11) NOT NULL,
`max_dictionaries` int(11) NOT NULL,
`max_dictionary_entries` int(11) NOT NULL,
`max_sentiment_models` int(11) NOT NULL,
`max_sentiment_entries` int(11) NOT NULL,
`current_credits` bigint(20) NOT NULL,
`current_requests` bigint(20) NOT NULL,
`last_operation` timestamp NULL DEFAULT NULL,
`total_credits` bigint(20) NOT NULL,
`total_requests` bigint(20) NOT NULL,
`msg_pay_status` varchar(510) COLLATE utf8_unicode_ci NOT NULL,
`start_date` timestamp NULL DEFAULT NULL,
`start_billing_date` timestamp NULL DEFAULT NULL,
`expiration_date` timestamp NULL DEFAULT NULL,
`update_operation` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,,
`updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

我要执行的查询是:

update `license_keys` set `id_nuevo` = 1, `updated_at` = '2018-11-15 13:24:11' where `hash_key` = '0...0b'

谢谢。

1 个答案:

答案 0 :(得分:1)

尝试不带update_at的查询,它将自动写入

import turtle
x1= -300
y1= 270

turtle.setup(600, 600)
turtle.speed(0)
turtle.bgcolor('black')
for y in range (1, 16):

    for x in range(10):
        turtle.penup()
        turtle.goto(x1, y1)
        turtle.pencolor('red')
        turtle.pendown()
        turtle.fillcolor('red')
        turtle.begin_fill()
        turtle.forward(50)
        turtle.left(90)
        turtle.forward(25)
        turtle.left(90)
        turtle.forward(50)
        turtle.left(90)
        turtle.forward(25)
        turtle.left(90)
        turtle.end_fill()
        x1 += 60
    if y % 2 == 0:
        x1 -= -200

    else:
        x1 = -300
        y1 -= 270