Laravel 迁移 - 创建时间戳时出现问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30555844/
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 11:40:43  来源:igfitidea点击:

Laravel Migrations - Issues while creating timestamps

phpmysqllaravelmigration

提问by cheese5505

I am trying to run migrations on my Laravel instance. They are just the default migrations (users and password resets) but when it tries to make the timestamps it throws this error:

我正在尝试在我的 Laravel 实例上运行迁移。它们只是默认迁移(用户和密码重置),但是当它尝试制作时间戳时会引发此错误:

 [Illuminate\Database\QueryException]
 SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'created_at' (SQL: create table `
 users` (`id` int unsigned not null auto_increment primary key, `name` varchar(255) not null, `email` varchar(255) n
 ot null, `password` varchar(60) not null, `remember_token` varchar(100) null, `created_at` timestamp default 0 not
 null, `updated_at` timestamp default 0 not null) default character set utf8 collate utf8_unicode_ci)

as well as a PDOException:

以及 PDOException:

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

How can I fix this?

我怎样才能解决这个问题?

Thanks.

谢谢。

采纳答案by Tom

This is due to MySQL not accepting zero as a valid default date and thus the table creation fails a constraint check on creation.

这是因为 MySQL 不接受零作为有效的默认日期,因此表创建未能通过创建约束检查。

You probably have NO_ZERO_DATEenabled in your MySQL configuration. Setting this to off will allow you to create the table or alternatively remove the default 0 value or change it to CURRENT_TIMESTAMP.

您可能已NO_ZERO_DATE在 MySQL 配置中启用。将此设置为 off 将允许您创建表,或者删除默认的 0 值或将其更改为CURRENT_TIMESTAMP.

You can find out more about this exact issue here: https://github.com/laravel/framework/issues/3602

您可以在此处找到有关此确切问题的更多信息:https: //github.com/laravel/framework/issues/3602

回答by Mohan Singh

I have been facing the same error. Given solutions does work properly still i want to help laravel developers. Simply add a following line to config/database.php

我一直面临同样的错误。鉴于解决方案确实工作正常,我仍然想帮助 Laravel 开发人员。只需将以下行添加到 config/database.php

'mysql' => array(
   'strict'    => true
),

回答by Imtiaz Pabel

it sounds like strict mode.

这听起来像严格模式。

You may disable strict mode in one of two ways:

您可以通过以下两种方式之一禁用严格模式:

Open your my.ini file within the MySQL installation directory, and look for the text sql-mode.

在 MySQL 安装目录中打开 my.ini 文件,并查找文本 sql-mode。

Find:

找:

sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

and change to

并更改为

sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

or you can run the following in phpMyAdmin

或者您可以在 phpMyAdmin 中运行以下命令

SET @@global.sql_mode='';

SET @@global.sql_mode='';

回答by Abdelwahid Oubaalla

This is due to MySQL not accepting zero as a valid default date so you can write

这是因为 MySQL 不接受零作为有效的默认日期,因此您可以编写

$table->timestamp('created_at')->nullable();
$table->timestamp('updated_at')->nullable();

or $table->nullableTimestamps();

或者 $table->nullableTimestamps();

Instead of $table->timestamps();

代替 $table->timestamps();

回答by Chris

This worked for me after being unsuccessful with strict mode:

在使用严格模式失败后,这对我有用:

$table->timestamp('published_on')->useCurrent();

回答by Priyanka Patel

You can use nullableTimestamps() instead of timestamps()

您可以使用 nullableTimestamps() 而不是 timestamps()

or else

要不然

$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 Anil Singh

I have used the following method:

我使用了以下方法:

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

Really worked!

真的有用!

回答by Harry Bosh

Migrating old tables works like that:

迁移旧表的工作方式如下:

Schema::table(
            'table',
            function (Blueprint $table) {
                $table->dateTime('created_at')->nullable()->default(NULL)->change();
                $table->dateTime('updated_at')->nullable()->default(NULL)->change();
            }
        );

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

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

回答by mohammadreza khalifeh

you should disable MySQL strict mode on Laravel. MySQL has had a strict mode since 5.1, but in 5.7 it became the default. In Laravel, you can fix this in code: edit your database.phpconfig file, and add a key of strictwith a value of false.

你应该在 Laravel 上禁用 MySQL 严格模式。MySQL 从 5.1 开始就有了严格模式,但在 5.7 中它成为默认模式。在 Laravel 中,您可以在代码中修复此问题:编辑您的database.php配置文件,并添加strict一个值为false.

for non-Laravel users:

对于非 Laravel 用户:

if you're using a non-Laravel application,you won't have that option.Here's how to disable strict mode globally.find your MySQL configuration file my.cnfor my.inithe default MySQL configuration will live in /etc/mysql/my.cnf

如果您使用的是非 Laravel 应用程序,则不会有该选项。这是全局禁用严格模式的方法。找到您的 MySQL 配置文件my.cnfmy.ini默认的 MySQL 配置将存在/etc/mysql/my.cnf

open the file and find the [mysqld]section.We're going to add a new key, sql_modeOn MySQL 5.7, the default values for this key out of the box are:

打开文件并找到该部分[mysqld]。我们将添加一个新键,sql_mode在 MySQL 5.7 上,此键的默认值是:

STRICT_TRANS_TABLES,ONLY_FULL_GROUP_BY,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

The strict mode comes from STRICT_TRANS_TABLES. So, let's overwrite the sql_modeto:

严格模式来自STRICT_TRANS_TABLES. 所以,让我们覆盖sql_mode到:

[mysqld]
sql_mode=ONLY_FULL_GROUP_BY,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

That's it! Save the file, and restart MySQL.

就是这样!保存文件,然后重新启动MySQL

回答by brad

MySQL 5.7.28

MySQL 5.7.28

The MySQL docsrecommend the following (note the use of GLOBAL):

MySQL 文档推荐以下内容(注意使用GLOBAL):

SET GLOBAL sql_mode = 'ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';