Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

orrd's avatar
Level 4

MySQL requires TEXT fields to have a default value in strict mode??

The new MySQL database default configuration for Laravel has 'strict' => true set. I think I like the idea of keeping that on to report on potential issues and encourage best practices.

But I'm running to a strange issue where I get the error "PDOException: SQLSTATE[HY000]: General error: 1364 Field 'foo' doesn't have a default value" when I try to save a model in that table without explicitly setting a value for that field.

But here's the crazy part, the field is a TEXT field, and MySQL doesn't allow text fields to have default values! So apparently the only solution is to always explicitly set the value for every text field whenever you create a new model object (database row)?

I could do that in each of the model classes by defining protected $attributes = [ 'your_text_field' => '' ];. But is that really the best solution to this problem?

0 likes
2 replies
clay's avatar
clay
Best Answer
Level 20

I usually see this error when I forget to mark a field as 'nullable' in the db. If you're using laravel migrations, then:

$table->text('foo')->nullable();
orrd's avatar
Level 4

Ah, yeah that may be the best solution. Often I prefer fields not be nullable so it isn't necessary to deal with the null case separately than ''. But I can see the logic in saying that if a text field can't have a default, and you don't specify a value, null is probably the only sensible default for it to have.

Thanks.

Please or to participate in this conversation.