我正在尝试在我的数据库表中添加一个新的整数“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 创建的。为此,我使用了带有以下代码的播种机:
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'
谢谢。
最佳答案
在没有 update_at 的情况下尝试你的查询,它会自动写入
update `license_keys` set `id_nuevo` = 1 where `hash_key` = '0...0b'
关于php - 在 Laravel 5.1 中添加带有迁移的列后,无法修改我的数据库中的 'updated_at',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53319962/