How do I allow an entity reference to be null in my custom entity? I am working on adding taxonomy to the poll module. This is my  BaseFieldDefinition:
$fields['category'] = BaseFieldDefinition::create('entity_reference')
  -&gt;setLabel(t('Category'))
  -&gt;setDescription(t('Section this poll is to appear on'))
  -&gt;setSetting('target_type', 'taxonomy_term')
  -&gt;setTranslatable(TRUE)
  -&gt;setDisplayOptions('form', array(
    'type' =&gt; 'entity_reference_autocomplete',
    'weight' =&gt; -10,
    'settings' =&gt; array(
      'match_operator' =&gt; 'CONTAINS',
      'size' =&gt; '60',
      'autocomplete_type' =&gt; 'tags',
      'placeholder' =&gt; '',
      'options_list_callback' =&gt; 'my_callback',
    ),
  ))
  -&gt;setDisplayConfigurable('form', TRUE)
  -&gt;setRequired(FALSE);

However, I would like the category to be optional, but when creating a poll if it is left blank it returns an SQL error and this error appears on screen.

The website encountered an unexpected error. Please try again later.
Drupal\Core\Entity\EntityStorageException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'category' cannot be null: INSERT INTO  {poll_field_data} (id, langcode, uid, question, category, runtime,   anonymous_vote_allow, cancel_vote_allow, result_vote_allow, status, created, default_langcode) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1,  :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4,  :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8, :db_insert_placeholder_9, :db_insert_placeholder_10, :db_insert_placeholder_11); Array ( [:db_insert_placeholder_0] =&gt; 3  [:db_insert_placeholder_1] =&gt; en [:db_insert_placeholder_2] =&gt; 1  [:db_insert_placeholder_3] =&gt; TEST poll [:db_insert_placeholder_4] =&gt;   [:db_insert_placeholder_5] =&gt; 0 [:db_insert_placeholder_6] =&gt; 0  [:db_insert_placeholder_7] =&gt; 1 [:db_insert_placeholder_8] =&gt; 0  [:db_insert_placeholder_9] =&gt; 1 [:db_insert_placeholder_10] =&gt; 1442410281 [:db_insert_placeholder_11] =&gt; 1 ) in Drupal\Core\Entity\Sql\SqlContentEntityStorage-&gt;save() (line 755 of core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).

If -&gt;setRequired(FALSE) doesn't allow null columns in the database what does?
The 'not null' instruction is automatically set to TRUE if that field is set in your entity's (entity_keys) annotations:

ex: 

entity_keys = {
 *     "id" = "id",
 *     "uuid" = "uuid",
 *     "uid" = "uid",
 *     "price" = "price",
 *     "langcode" = "langcode",
 *     "complete" = "complete",
 *   },


In this case -&gt;setRequired(FALSE) will be ignored.
However, if you really have to, you can force it to FALSE if you provide your custom storage schema handler.

Todo so add the handler in your entity annotations:

handlers = {
 *     "storage_schema" = "Drupal\YOUR_MODULE\YourEntityStorageSchema",
...
}


And the create YourEntityStorageSchema.php file:

&lt;?php

namespace Drupal\YOUR_MODULE;

use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema;
use Drupal\Core\Field\FieldStorageDefinitionInterface;

class YourEntityStorageSchema extends SqlContentEntityStorageSchema {

  protected function getEntitySchema(ContentEntityTypeInterface $entity_type, $reset = FALSE) {
    $schema = parent::getEntitySchema($entity_type, $reset);

    //then target your annoying field and set the 'not null' key to FALSE!
    if (!empty($schema['rp_payment']['fields']['operator_id']))
      $schema['rp_payment']['fields']['operator_id']['not null'] = FALSE;

    return $schema;
  }
}


Hope it helps! ^^