php - Laravel 迁移无法运行,但查询确实有效

标签 php mysql laravel laravel-5 laravel-5.4

我有一个无法运行的迁移。

public function up()
{
    Schema::create('users_watch_history', function (Blueprint $table) {
        $table->string('id', 36)->primary();
        $table->string('user_id', 36);
        $table->string('video_id', 36);
        $table->string('course_id', 36);
        $table->timestamp('last_timestamp');
        $table->float('vid_watch', 5, 2);
        $table->float('total_watch', 5, 2);
        $table->text('watched_parts');
        $table->integer('last_position');
        $table->softDeletes();

        $table->foreign('user_id')
            ->references('id')->on('users')
            ->onDelete('cascade')->onUpdate('cascade');

        $table->foreign('video_id')
            ->references('id')->on('videos')
            ->onDelete('cascade')->onUpdate('cascade');

        $table->foreign('course_id')
            ->references('id')->on('courses')
            ->onDelete('cascade')->onUpdate('cascade');
    });
}

当迁移实际运行时,我不断收到有关 last_timestamp 列默认值无效的错误消息。

$ php artisan migrate


  [Illuminate\Database\QueryException]
  SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid
  default value for 'last_timestamp' (SQL: create table 
  `users_watch_history` (`id` varchar(36) not null, `user_id` 
  varchar(36) not null, `video_id` varchar(36) not null, `course_id` 
  varchar(36) not null, `last_timestamp` timestamp not null, 
  `vid_watch` double(5, 2) not null, `total_watch` double(5, 2) not 
  null, `watched_parts` text not null, `last_position` int not null, 
  `deleted_at` timestamp null) default character set utf8 collate utf8_unicode_ci)



  [PDOException]
  SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'last_timestamp'

我的 MySQL 版本

> select @@version
+------------+
| @@version  |
+------------+
| 5.6.34-log |
+------------+
1 row in set (0.20 sec)

但让我陷入困境的是 - 如果我在 CLI 中为数据库运行创建命令,一切正常吗?

> create table `users_watch_history` (`id` varchar(36) not null, `user_id` varchar(36) not null, `video_id` varchar(36) not null, `course_id` varchar(36) not null, `last_timestamp` timestamp not null, `vid_watch` double(5, 2) not null, `total_watch` double(5, 2) not null, `watched_parts` text not null, `last_position` int not null, `deleted_at` timestamp null) default character set utf8 collate utf8_unicode_ci;
Query OK, 0 rows affected (0.19 sec)

表架构:

> show create table users_watch_history \G
*************************** 1. row ***************************
       Table: users_watch_history
Create Table: CREATE TABLE `users_watch_history` (
  `id` varchar(36) COLLATE utf8_unicode_ci NOT NULL,
  `user_id` varchar(36) COLLATE utf8_unicode_ci NOT NULL,
  `video_id` varchar(36) COLLATE utf8_unicode_ci NOT NULL,
  `course_id` varchar(36) COLLATE utf8_unicode_ci NOT NULL,
  `last_timestamp` timestamp NOT NULL,
  `vid_watch` double(5,2) NOT NULL,
  `total_watch` double(5,2) NOT NULL,
  `watched_parts` text COLLATE utf8_unicode_ci NOT NULL,
  `last_position` int(11) NOT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
1 row in set (0.17 sec)

知道是什么导致此有效查询无法通过迁移运行吗?

最佳答案

禁用 MySQL 严格模式。

  • 在你的 laravel 配置文件 database.php 中,设置 'strict' => true 到 'strict' => false
  • 或者添加nullable()$table->timestamp('last_timestamp')->nullable();

    其中之一 应该可以解决问题。

关于php - Laravel 迁移无法运行,但查询确实有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45717944/

相关文章:

php - 如何在 laravel 中验证字数

php - mysql中的计数器不起作用

php - 嵌套查询和计算都在同一个查询中 - 这可能吗?

sql - 将数据附加到已包含数据的 MySQL 数据库字段

php - 身份验证类不适用于 Laravel 中的 Traits

html - 导航菜单中的内容被推到左边

php - Composer - 请求包 [0.0.9] 作为 [0.0.5,0.0.6] 存在,但这些被您的约束拒绝

php - Laravel/Eloquent 5 事务回滚不支持保存

mysql - 如何使用选择值表从列值中动态选择后缀值

mysql - 通过 TIMESTAMP 类型从 MYSQL 表中选择最近 24 小时内创建的记录