laravel 语法错误或访问冲突:1067 'created_at' 的默认值无效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45935076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:34:21  来源:igfitidea点击:

Syntax error or access violation: 1067 Invalid default value for 'created_at'

phpmysqllaravellaravel-5

提问by paranoid

I install laravel 5.5 and When I run php artisan migrateshow me this error

我安装了 laravel 5.5,当我运行时php artisan migrate显示这个错误

[Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQ L: alter table usersadd unique users_email_unique(email))

[Illuminate\Database\QueryException] SQLSTATE[42000]:语法错误或访问冲突:1071 指定的键太长;最大密钥长度为 767 字节(SQL:alter table usersadd unique users_email_unique( email))

And I add below code on AppServiceProvider.php

我在上面添加以下代码 AppServiceProvider.php

 public function boot()
{
     Schema::defaultStringLength(191); //Solved by increasing StringLength
}

And then show me this error

然后给我看这个错误

[Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'created_at' (SQL: create table password_resets(emailvarchar(191) not null, tokenvarchar(191) not null, created_attimestamp not null) de fault character set utf8mb4 collate utf8mb4_unicode_ci)

[Illuminate\Database\QueryException] SQLSTATE[42000]:语法错误或访问冲突:1067 'created_at' 的默认值无效(SQL:创建表password_resetsemailvarchar(191) not null,tokenvarchar(191) not null,created_attimestamp not null)默认字符集 utf8mb4 整理 utf8mb4_unicode_ci)

回答by Jigar Shah

You can use nullableTimestamps()instead of timestamps()

您可以使用nullableTimestamps()代替timestamps()

or

或者

$table->timestamp('created_at')->default(\DB::raw('CURRENT_TIMESTAMP'));

also check the database server version

还要检查数据库服务器版本

Please have a look on these ref links:

请查看这些参考链接:

https://github.com/laravel/framework/issues/3602

https://github.com/laravel/framework/issues/3602

https://laracasts.com/discuss/channels/forge/syntax-error-or-access-violation-1067-invalid-default-value-for-created-at

https://laracasts.com/discuss/channels/forge/syntax-error-or-access-violation-1067-invalid-default-value-for-created-at

回答by Apit John Ismail

I'm currently using laravel 7.

我目前正在使用 laravel 7。

I just go to config/database.php

我只是去 config/database.php

Then I change strict => trueto strict => false.

然后我更改strict => truestrict => false.

Then run the same migration again. It works.

然后再次运行相同的迁移。有用。

回答by saber tabatabaee yazdi

my case

我的情况

        $table->timestamp('deleted_at')->nullable()->default(null);

this worked

这有效