Vince Parker
Vince Parker

Reputation: 173

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

Im using Laravel framework and it gives an error with the 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 ...

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.

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

Upvotes: 4

Views: 13239

Answers (2)

Adam
Adam

Reputation: 3518

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

and then php artisan migrate

Upvotes: 1

Alex Andrei
Alex Andrei

Reputation: 7283

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

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

You can use this sql statement to alter the table

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

Or do it from PhpMyAdmin

Upvotes: 1

Related Questions