Not sure why, but all the way up to the initial commit of this module, we have had target_id
defined as:
'target_id' => array(
'description' => 'The id of the target entity',
'type' => 'int',
'unsigned' => TRUE,
'not null' => FALSE,
),
The column accepts NULL as a value, which is legitimate in some corner cases (the Tree module uses this to store roots of the tree, but it already can modify the schema itself), but most of the cases it just hides obscure bugs.
For example in #1569046: Handling NULL values during the migration of Entity Reference fields, we have a case of a migration storing NULL values in there incorrectly, which result in:
The target_id => NULL causes all kinds of EntityMalformedExceptions, hinders indexing by Solr and causes this kind of warnings: Warning: array_flip() [function.array-flip]: Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load()
I think we should just consider moving to NOT NULL and this type of issue would have been caught way sooner.
Comment | File | Size | Author |
---|---|---|---|
#7 | 1569144-target-id-not-null.patch | 617 bytes | andreypaa |
#6 | 1569144-target-id-not-null.patch | 617 bytes | andreypaa |
#3 | 1569144-target-id-not-null.patch | 1.59 KB | Damien Tournoud |
Comments
Comment #1
amitaibu> we have a case of a migration storing NULL values in there incorrectly
I think we should follow core here, and keep it not NULL:
although maybe I miss a case where it's indeed needed to be NULL?
Comment #2
amitaibuoh, I've misread the title -- yes it should be
'not NULL' => TRUE
Comment #3
Damien Tournoud CreditAttribution: Damien Tournoud commentedOk, changing this is relatively trivial. Here is a patch.
Comment #4
amitaibuComment #5
Damien Tournoud CreditAttribution: Damien Tournoud commentedMerged into 7.x-1.x.
Comment #6
andreypaa CreditAttribution: andreypaa commentedCorrect update process.
Comment #7
andreypaa CreditAttribution: andreypaa commentedCorrect update process.
Sorry for the double comment
Comment #8
gaele CreditAttribution: gaele commentedRight, update fails in rc2 (Missing argument 4 for db_change_field()). Patch from #7 solves this.
Comment #9
pandaPowder CreditAttribution: pandaPowder commentedThanks for the patch. Upgrade with drush now works
Comment #10
Damien Tournoud CreditAttribution: Damien Tournoud commentedShame. I was certain I tested that. Let's roll a rc3 :(
Comment #12
j0rd CreditAttribution: j0rd commentedI run into cases where node_save's happen on nodes that were created programatically with out initializing the entityfields in the creation.
Then if I load and attempt to programatically save, I run into "field_entity_reference" cannot be null database fails.
So I think if we're going to make these not null, then perhaps we need to make sure on node_load or node_presave that entity_reference will not cause these NULL inserts.
UPDATE: Actually perhaps these nodes were created before I added this particular field_entity_reference to them. Thus the fields are not initialized. Either way, need to make sure we can save them.