首页 > 解决方案 > 如何将 CURRENT_TIMESTAMP 设置为列的默认值

问题描述

使用 laravel 迁移,我想将CURRENT_TIMESTAMPcreated_at 列设置为默认值。

不知道是不是把laravel从5.7迁移到了6

这是我的迁移文件:

Schema::create('activities', function (Blueprint $table) {

        $table->engine = 'InnoDB';
        $table->charset = 'utf8mb4';
        $table->collate = 'utf8mb4_unicode_ci';

        $table->bigIncrements('id')->unsigned();
        $table->unsignedInteger('user_id')->nullable()->index('idx_user_id');
        $table->string('ip', 50);
        $table->string('user_agent', 255);
        $table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));

    });

控制台中的错误:

Doctrine\DBAL\Driver\PDOException::("SQLSTATE[42000]: 语法错误或访问冲突:1064 您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本相对应的手册以获取在 ' 附近使用的正确语法(CURRENT_TIMESTAMP)) 默认字符集 utf8mb4 collat​​e 'utf8mb4_unicode_ci' ' at line 1")

mysql版本:5.7.23

PHP版本:7.2.10

Laravel 版本:6.0.2

标签: laraveldatabase-migrationlaravel-6

解决方案


推荐阅读