MySQL fix: Field ‘xxxx’ doesn’t have a default value

I’ve recently been working on a few of our older websites with newer MySQL installations and am coming across the following error:

This is caused by MySQL having a strict mode set which won’t allow INSERT or UPDATE commands with empty fields where the schema doesn’t have a default value set.

There are a couple of fixes for this.

First ‘fix’ is to assign a default value to your schema. This can be done with a simple ALTER command:

However, this may need doing for many tables in your database schema which will become tedious very quickly. The second fix is to assign a default sql_mode on the mysql server.

If you are using a brew installed MySQL you should edit the my.cnf file in the MySQL directory at /usr/local/Cellar/mysql/<version>/my.cnf. Comment out or change the sql_mode at the bottom:

Save the file and restart Mysql:

NOTE: make sure you run the above as your usual user and not as root/sudo. I did this and MySQL refused to restart as I had caused it to break permissions somewhere.

Alternatively you can comment out the above line and add the sql_mode line to your system MySQL config at /etc/my.cnf:

Hope this helps someone out there!