The function db_field_set_default takes four arguments, the last one being the default value. If $default is set to integer 0 (or $default == NULL), db_field_set_default attempts to set the default value to NULL. This is due to the if clause that tests whether $default is NULL and if so, sets it to 'NULL'. Since the if clause doesn't do type testing, values such as '' and 0 get replace by NULL. This results in query errors in case the field in question does not allow NULL.
OS: Mac OS X 10.5.7
Apache/2.0.59 (Unix) PHP/5.2.5 DAV/2
Drupal: 6.13
Drupal path: ~/Sites/projectname/web/www
Repeatable: Always
Steps to repeat:
1. Create a schema definition with at least one field that doesn't allow NULL.
2. Run db_field_set_default(), passing 0 (integer) as the fourth argument.
3. You should get the database error above. Expected outcome should be
Expected Results:
Query is run successfully: ALTER TABLE table ALTER COLUMN field SET DEFAULT 0
Actual results:
Query results in error: user warning: Invalid default value for 'field' query: ALTER TABLE table ALTER COLUMN field SET DEFAULT NULL in drupal/includes/database.mysql-common.inc on line 345.
I discovered this while writing database updater for one of my modules. I've attached a patch that I've tested locally which fixes the bug.
| Comment | File | Size | Author |
|---|---|---|---|
| db_field_set_default_0_as_null.patch | 681 bytes | solipsist |
Comments
Comment #1
TBarregren commentedI've reviewed the patch. I agree that it is a bug, and that the patch solves it.
Comment #2
lyricnz commentedI agree, ran into this bug also - and applied same fix.
Comment #3
lyricnz commentedMarking RTBC
Comment #4
gábor hojtsyThanks, committed.