Hey guys,

I created a content type with a text field that now I have to change the maximum length for, how can I do this if the content type already has records on it and it's telling me this cannot be changed when it already has records?

Thanks.

Comments

drupalshrek’s picture

I don't think there is a simple way.

One way which I suppose should work: backup database, delete records, change field size, reimport data (not structure) from backup.

drupalshrek

jfha73’s picture

I did, but it still shows the wrong value, I also looked in the database for the content typy where it is, but I can't find it, can anybody tell me where Drupal 7 stores this info (content types) in the database? so I can check if this is there?

Thanks,

VM’s picture

node types are stored in the node types table - non core fields are stored in their own tables.

jfha73’s picture

I found the table, but there is nothing on it that says field lengths, so I'm back to square 1, so if the table for the field in the database has the right size on it, how can I make my content type to show/allow this size, not the previous size?

Thanks,

jfha73’s picture

THANKS A MILLION, that worked.

stergueaw’s picture

nitin.k’s picture

Install devel, go to URL(http://sitename/devel/php) and run the code in text editor.


<?php

/**
  * Changes the field maximum length.
  */

// Provide field name and length for the field.
$field_name = "field_url_text";
$new_length = 500;  


$field_table = 'field_data_' . $field_name;
$field_revision_table = 'field_revision_' . $field_name;
$field_column = $field_name . '_value';
 
  // Alter value field length in fields table
  db_query("ALTER TABLE `{$field_table}` CHANGE `{$field_column}` `{$field_column}` VARCHAR( {$new_length} )");
  // Alter value field length in fields revision table
  db_query("ALTER TABLE `{$field_revision_table}` CHANGE `{$field_column}` `{$field_column}` VARCHAR( {$new_length} )");
 
  // Update field config with new max length
  $result = db_query("SELECT CAST(`data` AS CHAR(10000) CHARACTER SET utf8) FROM `field_config` WHERE field_name = '{$field_name}'");
  $config = $result->fetchField();
  $config_array = unserialize($config);
  $config_array['settings']['max_length'] = $new_length;
  $config = serialize($config_array);
  db_update('field_config')
    ->fields(array('data' => $config))
    ->condition('field_name', $field_name)
    ->execute();
?>

crawley’s picture

IT WORKED

goodmuyis’s picture

For Some reasons this work on localhost (Acquia Dev Stack) with success message status but did now work on Live server and no status message. I have to backup DB to Localhost, run the code, then restore back online

DLZJ’s picture

Hi Nitin K

Is this going to work in Drupal 8 as well? Thanks

Deepak Goyal’s picture

You can set max length of any filed in jquery

$(".form-item-title input").attr('maxlength','40');