laravel MySQL - “字段”没有默认值

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

MySQL - 'field' doesn't have a default value

phpmysqllaravel

提问by Vince Parker

Im using Laravel framework and it gives an error with the DB

我正在使用 Laravel 框架,但它在数据库中出现错误

Error message:

错误信息:

[2016-04-25 06:07:34] local.ERROR: exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 1364 Field 'remarks' doesn't have a default value' in ...

[2016-04-25 06:07:34] local.ERROR: 异常 'PDOException' 带有消息 'SQLSTATE[HY000]: 一般错误:1364 字段 'remarks' 在 ...

The field 'remarks' has a default value of 'None' set in PHPMyAdmin. I dont understand why does it gives an error when it has a default value set. I believe that 'None' is a string value so it's not like a NULL value.

'remarks' 字段在 PHPMyAdmin 中设置了默认值 'None'。我不明白为什么它在设置了默认值时会出错。我相信 'None' 是一个字符串值,所以它不像一个 NULL 值。

$aId = DB::table('attachments')->insertGetId([ 'document_type_code'=>$document_id, 'report_no'=>'report '.$document_id, 
'file_attachment_link'=>$filepath, 'file_attachment_upload'=>$file->getClientOriginalName(), 'uploaded_at'=> $now, 'uploaded_by' => 1, 
//Auth::user()->id 'version_number' => 1, ]);

回答by Alex Andrei

Noneis not a default string value. It means that there is no default value set.

None不是默认字符串值。这意味着没有设置默认值。

You can either pass a value in your INSERTstatement or alter the table to actually hold a default value for the column.

您可以在INSERT语句中传递一个值,也可以更改表以实际保存该列的默认值。

You can use this sqlstatement to alter the table

您可以使用此sql语句来更改表

ALTER TABLE attachments MODIFY COLUMN `remarks` VARCHAR(255) DEFAULT 'something';

Or do it from PhpMyAdmin

或者从 PhpMyAdmin

回答by Adam

Don't edit the tables directly. You have your model for that. Generate a new migration and set you field to be nullable:

不要直接编辑表格。你有你的模型。生成新的迁移并将您的字段设置为可为空:

$table->string('name', 50)->nullable();

$table->string('name', 50)->nullable();

and then php artisan migrate

进而 php artisan migrate