0

I have a custom field type that I'm trying to add to a custom fieldable entity. The entity works fine and I've added other fields to it, my problem is when I go to add an instance of my field to the entity, I get the following SQL error:

There was a problem creating field Authors: Exception thrown while performing a schema update.
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NOT NULL, PRIMARY KEY (entity_id, deleted, delta, langcode), INDEX bundle' at line 9: CREATE TABLE {myentity__field_myfield} ( bundle VARCHAR(128) CHARACTER SET ascii COLLATE ascii_general_ci NOT NULL DEFAULT '' COMMENT 'The field instance bundle to which this row belongs, used when deleting a field instance', deleted TINYINT NOT NULL DEFAULT 0 COMMENT 'A boolean indicating whether this data item has been deleted', entity_id INT unsigned NOT NULL COMMENT 'The entity id this data is attached to', revision_id INT unsigned NOT NULL COMMENT 'The entity revision id this data is attached to', langcode VARCHAR(32) CHARACTER SET ascii COLLATE ascii_general_ci NOT NULL DEFAULT '' COMMENT 'The language code for this data item.', delta INT unsigned NOT NULL COMMENT 'The sequence number for this data item, used for multi-value fields', field_myfield_target_uid INT unsigned NOT NULL COMMENT 'The user ID of the author', field_myfield_affiliation NOT NULL, PRIMARY KEY (entity_id, deleted, delta, langcode), INDEX bundle (bundle), INDEX revision_id (revision_id) ) ENGINE = InnoDB DEFAULT CHARACTER SET utf8mb4 COMMENT 'Data storage for myentity field field_myfield.'; Array ( )

I've replaced my entity with myentity and my custom field type with myfield for the above. My schema definition for my field type is as follows:

/**
 * {@inheritdoc}
 */
public static function schema(FieldStorageDefinitionInterface 
$field_definition) {

  $columns = [
    // List the values that the field will save. This
    // field will only save a single value, 'value'
    'target_uid' => [
      'type' => 'int',
      'not null' => true,
      'unsigned' => true,
      'description' => "The user ID of the author"
    ],
    'affiliation' => [
      'type' => 'varchar',
      'size' => 255,
      'not null' => true
    ]
  ];

  $schema = [
    'columns' => $columns,
    // 'indexes' => [
    //   'target_uid'
    // ],
    // 'foreign keys' => [
    //   'target_uid'
    // ],
  ];

  return $schema;
}

I've looked at other core field types for guidance but I can't see where I'm going wrong.

2

You’re confusing the required length property of a varchar field with the size property of a text field.

Replace size with length (or varchar with text if that makes more sense) and your code should start working.

  • 1
    Can I hug you? I'm pretty sure this is the 3rd time you've saved me now – Josh Brown Aug 9 '18 at 15:01

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.