orrd
2 years ago
477
2
Laravel

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

Posted 2 years ago by orrd

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?

Please sign in or create an account to participate in this conversation.