MySQL -“字段”没有默认值

时间:2021-12-14 17:10:17

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

我使用Laravel框架,它会在DB上出错

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当地。错误:异常'PDOException'与消息'SQLSTATE[HY000]:一般错误:1364字段' comments '在…

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.

在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, ]);

2 个解决方案

#1


1  

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

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

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

您可以在INSERT语句中传递一个值,也可以修改表来实际为该列保留一个默认值。

You can use this sql statement to alter the table

可以使用这个sql语句修改表

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

Or do it from PhpMyAdmin

或者是PhpMyAdmin。

#2


1  

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();

$表- >字符串(“名字”,50)- > nullable();

and then php artisan migrate

然后php技工迁移

#1


1  

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

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

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

您可以在INSERT语句中传递一个值,也可以修改表来实际为该列保留一个默认值。

You can use this sql statement to alter the table

可以使用这个sql语句修改表

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

Or do it from PhpMyAdmin

或者是PhpMyAdmin。

#2


1  

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();

$表- >字符串(“名字”,50)- > nullable();

and then php artisan migrate

然后php技工迁移