I have set a field in my entity to not required using


However when I then leave the field blank and try to save the entity I get this error

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 {myentity_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] => 1 [:db_insert_placeholder_1] => en [:db_insert_placeholder_2] => 1 [:db_insert_placeholder_3] => test [:db_insert_placeholder_4] => [:db_insert_placeholder_5] => 0 [:db_insert_placeholder_6] => 1 [:db_insert_placeholder_7] => 1 [:db_insert_placeholder_8] => 1 [:db_insert_placeholder_9] => 1 [:db_insert_placeholder_10] => 1442607827 [:db_insert_placeholder_11] => 1 ) in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 930 of core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).

So is this a result of setRequired or some failure of my entity reference?

  • Welcome to Drupal Answers. May you show the code containing that line? In this way, the question would be helpful to future users too.
    – apaderno
    Sep 19 '15 at 5:46
  • Have you reinstalled the module after you changed it?
    – user21641
    Sep 19 '15 at 10:33
  • Original question: drupal.stackexchange.com/questions/174603/…
    – Daniel
    Sep 20 '15 at 6:13

Your field was likely created in the database with setRequired(TRUE) which means your field cannot contain NULL values. If you change your field to setRequired(FALSE), then NULL values will be allowed.

Inspect your *_field_data table and check if the field schema IS_NULL = TRUE, if not, then you will need to update your field schema by running an update hook. See Write update functions for entity schema updates for information on how to update your entity schema.

If you have not deployed your module anywhere other than locally then it should be safe to modify your database directly without creating an update hook.


The 'not null' instruction is automatically set to TRUE if that field is set in your entity's (entity_keys) annotations:


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

In this case ->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:


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! ^^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.