Solved: MySQL fix for "Field ‘xxxx’ doesn’t have a default value"
June 22nd, 2015
Warning: This post is 8 years old. Some of this information may be out of date.
I've recently been working on a few of our older websites with newer MySQL installations and am coming across the following error:
SQLSTATE[HY000]: General error: 1364 Field 'delivery_address_id' doesn't have a default value
This is caused by MySQL having a strict mode set which won't allow
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 TABLE `details` CHANGE COLUMN `delivery_address_id` `delivery_address_id` INT(11) NOT NULL DEFAULT 0 ;
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:
# For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html [mysqld] # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin # These are commonly set, remove the # and set as required. # basedir = ..... # datadir = ..... # port = ..... # server_id = ..... # socket = ..... # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2M #sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES sql_mode=""
Save the file and restart Mysql:
launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
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
[mysqld] sql_mode = ""
Hope this helps someone out there!