Search code examples
drupaldrupal-7

Drupal add field varchar issues


I am using the following code to amend a database table by adding extra columns:

$componentName = 'is_this_the_first_time_you_have_taken_part_in_or_attended_this_event';
    db_add_field('webform_views_data',
        $componentName,
            array('type' => 'varchar', 'length' => 255, 'not null' => TRUE)
    );

However, I keep getting the following error:

> PDOException: SQLSTATE[42000]: Syntax error or access violation: 1059
> Identifier name
> 'is_this_the_first_time_you_have_taken_part_in_or_attended_this_event'
> is too long: ALTER TABLE {webform_views_data} ADD
> `is_this_the_first_time_you_have_taken_part_in_or_attended_this_event`
> VARCHAR(255) NULL DEFAULT NULL; Array ( ) in db_add_field() (line 2812
> of /var/www/mysite/includes/database/database.inc).

Regarding the obvious length detail in the error message, I think we can all agree 'is_this_the_first_time_you_have_taken_part_in_or_attended_this_event' is not 255 characters long.

Not sure what the issue is.


Solution

  • VARCHAR(255) or 255 characters is the maximum characters for the content of the field but the error points out that the name of the field is too long.

    That is, is_this_the_first_time_you_have_taken_part_in_or_attended_this_event is too long for a field name.

    See this answer for a related question and note that the character limit for your field name is 64 characters as per MySQL documentation.