When attempting to add a new column to a table within an update hook, the error Null value not allowed: 1138 Invalid use of NULL value
occurred, the definition was changed to allow NULL, and the value for existing rows was set to NULL.
function MYMODULE_update_8101() {
Drupal::database()->schema()->addField(
'MYMODULE_table',
'created',
[
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
]
);
}
When adding the column to the database with an SQL query, using the NOT NULL
restriction causes the field to have a default value of 0, and no error happens.
How can the error be fixed, and the proper schema set?
'null' => FALSE,
by removing'not null' => TRUE,
– vgoradiya Jan 4 '16 at 5:41'not null'
restriction is a valuable and required attribute of the column and simply removing it does not actually fix the problem, it just masks the error. There's a similar reason for not providing a'default'
attribute. Using'initial'
is the correct approach, but unfortunately not very discoverable from searching based on the error message. – gapple Jan 8 '16 at 21:31