Problem/Motivation
The update hook that was introduced in #3335488: Table entity string IDs should be 255 is causing a fatal database error:
SQLSTATE[22004]: Null value not allowed: 1138 Invalid use of NULL value: ALTER TABLE "entity_usage" CHANGE `target_id_string` `target_id_string` VARCHAR(255) CHARACTER SET ascii COLLATE ascii_general_ci NOT NULL DEFAULT '' COMMENT 'The target ID, when the entity uses string IDs.'
This is happening because the target ID column used to allow NULL values prior to 2.0-alpha5. This was changed in #2980089: Not working with configuration reference, but no update hook was supplied to update the pre-existing rows that contain NULL values.
There has been an earlier attempt to update the column to use NOT NULL and this resulted in exactly the same error, ref. #2981394: Update failed: entity_usage_update_8203.
Steps to reproduce
- Install Entity Usage 2.0-alpha4.
- Recreate the entity usage statistics.
- You probably now have thousands of entries where the value
NULLexists in thetarget_id_stringcolumn. - Update to 2.0-beta8.
Result: the error above is thrown.
Proposed resolution
Update all entries from the table that have a NULL value for the target_id_string column before declaring it to be NOT NULL:
UPDATE entity_usage SET target_id_string = '' WHERE target_id_string IS NULL;
| Comment | File | Size | Author |
|---|---|---|---|
| #8 | Upload fails on simply.com_.pdf | 210.63 KB | uv516 |
| #2 | 3340004-2.patch | 1 KB | pfrenssen |
Comments
Comment #2
pfrenssenComment #4
marcoscanoThanks for filing the ticket and for the fix!
Committed and tagged beta10 with this fix, since it's better to avoid beta9 at this point.
https://www.drupal.org/project/entity_usage/releases/8.x-2.0-beta10
Comment #5
pfrenssenThanks for the quick response!
Comment #7
szato commentedOn entity_usage_update_8206() I got:
I have to disable 'sql_require_primary_key' on session level by adding code to entity_usage_update_8206():
$database->query('SET SESSION sql_require_primary_key=0;');before calling:
Comment #8
uv516 commentedI got the error as descriped in attached file. It handles about the field "tagert_id_string" which doesn't have a default value.
After a lot of searching I found the error and the solution:
In MySQL under entity_usage I inspected the table.
The field "target_id_string" is created with no NULL-value and no default value.
The field can not have at NULL-value, but it MUST have at default value.
I set (in) 0 as the default value and that solved my problem.
From the patch:
I think that line 3
('->condition('target_id_string', NULL, 'IS NULL')')should be different?