Download & Extend

Consider making target_id NOT NULL

Project:Entity reference
Version:7.x-1.x-dev
Component:Code
Category:bug report
Priority:critical
Assigned:Unassigned
Status:closed (fixed)

Issue Summary

Not sure why, but all the way up to the initial commit of this module, we have had target_id defined as:

<?php
       
'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.

Comments

#1

> 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:

  $schema['taxonomy_term_data'] = array(
    'description' => 'Stores term information.',
    'fields' => array(
      'tid' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'Primary Key: Unique term ID.',
      ),

although maybe I miss a case where it's indeed needed to be NULL?

#2

oh, I've misread the title -- yes it should be 'not NULL' => TRUE

#3

Status:active» needs review

Ok, changing this is relatively trivial. Here is a patch.

AttachmentSizeStatusTest resultOperations
1569144-target-id-not-null.patch1.59 KBIdlePASSED: [[SimpleTest]]: [MySQL] 66 pass(es).View details

#4

Status:needs review» reviewed & tested by the community

#5

Status:reviewed & tested by the community» fixed

Merged into 7.x-1.x.

#6

Correct update process.

AttachmentSizeStatusTest resultOperations
1569144-target-id-not-null.patch617 bytesIdlePASSED: [[SimpleTest]]: [MySQL] 66 pass(es).View details

#7

Status:fixed» needs review

Correct update process.
Sorry for the double comment

AttachmentSizeStatusTest resultOperations
1569144-target-id-not-null.patch617 bytesIdlePASSED: [[SimpleTest]]: [MySQL] 66 pass(es).View details

#8

Priority:normal» critical
Status:needs review» reviewed & tested by the community

Right, update fails in rc2 (Missing argument 4 for db_change_field()). Patch from #7 solves this.

#9

Thanks for the patch. Upgrade with drush now works

#10

Status:reviewed & tested by the community» fixed

Shame. I was certain I tested that. Let's roll a rc3 :(

#11

Status:fixed» closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.