Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I have set once to a website a lenght limit to a field. And now the client wants to put more characters to that field.

I cannot change the maximum size from Drupal because I get the following error message:

There is data for this field in the database. The field settings can no longer be changed.

However there must be a solution. Shall I change it in database (the field is one implemented by the Field-collections module)?

share|improve this question
Can you tell us what version of Drupal and CCK? Also any CCK contrib modules? – pthurmond Aug 8 '11 at 20:26
Drupal version is 7. – Ek Kosmos Aug 9 '11 at 6:16
And you can't modify it under the content type manage fields section? You should be able to modify it at any point. It might give you a warning, but it should still allow it. This may be bug/limitation in the field collections module. – pthurmond Aug 9 '11 at 14:15
Have you tried using field groups? You can now do things like group duplication that you could not before. – pthurmond Aug 9 '11 at 14:15
I can't modify it under the content type manage fields section! This is the point. Drupal do not let me do it. – Ek Kosmos Aug 10 '11 at 10:50
show 2 more comments

5 Answers

up vote 29 down vote accepted
+50

Dylan Tack solution is the easiest, but personally I enjoy exploring the inner wards of Drupal's database to seen how things are managed down there.

So, assuming you have a text field which machine name is field_text of 10 characters you want to grow to 25:

  • data will be stored in two tables: field_data_field_text and field_revision_field_text
  • definition is stored in field_config for the storage data, and field_config_instance for each instance of this field (stuff like label).

Now let's do a little heart surgery.

  1. Alter the data tables columns definitions:

    ALTER TABLE `field_data_field_text` 
    CHANGE `field_text_value` `field_text_value` VARCHAR( 25 ) 
    CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL;
    
    ALTER TABLE `field_revision_field_text` 
    CHANGE `field_text_value` `field_text_value` VARCHAR( 25 ) 
    CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL;
    
  2. Change the definition column, this one is very tricky because it's stored in a BLOB, but that's not something that will stop you for doing this.

    • Dissect the guts of this BLOB thing:

    SELECT CAST(`data` AS CHAR(10000) CHARACTER SET utf8) 
    FROM `field_config` WHERE field_name = 'field_text';
    
    • This will give you something like:

    a:7:{s:12:"translatable";s:1:"1";s:12:"entity_types";a:0:{}s:8:"settings";a:2:
        {s:10:"max_length";s:2:"10";s:17:"field_permissions";a:5:
        //a lot more stuff...
    
    • This is a PHP serialized array, the interesting part is s:10:"max_length";s:2:"10";, this mean this array has a property named max_length (which name is a 10 characters string - hence the "s") which value is 10 (which is a 2 characters long string). It's pretty easy, isn't it?

    • Changing its value is as easy as replacing the s:2:"10" part by s:2:"25". Be careful: if your new value is longer you have to adapt the "s" part, for example putting 100 will be s:3:"100" as 100 length is 3.

    • Let's put this new value back in the DB, don't forget to keep the whole string.

    UPDATE `field_config` 
    SET data = 'a:7:{...a:2:{s:10:"max_length";s:2:"25";...}'
    WHERE `field_name` = 'field_text'
    
    • Flush your caches.
  3. ???

  4. PROFIT!

By the way, PhpMyAdmin has some setting to allow direct modification of BLOB columns, but why go the easy way?

PS: This can also save your life when putting some PHP code in views and getting a WSOD because of an error in your code.

share|improve this answer
That first SQL statement should say ALTER TABLE not SELECT TABLE. Also, you can make it cleaner like: ALTER TABLE field_data_field_text MODIFY field_text_value... (MODIFY is like CHANGE when you don't want to change the name. And you don't.) – artfulrobot Feb 2 '12 at 10:16
1  
Thanks for this. I wish I had more than one upvote to give. – BC01 Feb 9 '12 at 19:33
1  
+1 for being helpful. Would give another for step's 3 and 4 though. – SpaceBeers Mar 7 '12 at 10:10
Great! Thank you very much! – Jenechka May 25 '12 at 10:35

This appears to be a limitation of the current text module. text_field_settings_form() has this comment: "@todo: If $has_data, add a validate handler that only allows max_length to increase.".

As a temporary workaround, you could comment out '#disabled' => $has_data in modules/field/modules/text/text.module, around line 77.

I couldn't find an existing issue for this specific case, but you might mention it on #372330.

share|improve this answer
1  
Thanks for your answer. I have comment the line and now I can modify the value form the field, however I get the following error after sumbit: Attempt to update field Serial No failed: field_sql_storage cannot change the schema for an existing field with data.. – Ek Kosmos Aug 27 '11 at 8:36
Concerning committing the drupal issue, please go ahead and do it. – Ek Kosmos Aug 27 '11 at 8:37

In Drupal 7, I made modifications to 2 tables in phpmyadmin, and refreshed the cache.

  1. "field_data_field_lorem": changed "field_lorem_value" to "longtext"
  2. "field_config": changed "type" to "text_long"
share|improve this answer
Did you submit this as a patch to D7? If this is truly the issue then you should really issue a patch to them to resolve this issue in the core code. – pthurmond Aug 15 '11 at 15:32
I have changed my the field size value from phpmyadmin(from 10 to 25 of type VARCHAR), and refreshed the cache, however is not working. In database ramained VARCHAR(25) but in Drupal, the field is at the same maximum size value as before 10. Maybe there is some hidden field setting? – Ek Kosmos Aug 16 '11 at 13:37

Try this... this will update the field data type from TEXT to LONG_TEXT and update the max_length from 4000 to max long text length... hope this helps.

function my_module_update_1001(&$sandbox) { // Check if our field is already created. if (field_info_field('sample_text_field')) { db_query('ALTER TABLE field_data_sample_text_field CHANGE sample_text_field_value sample_text_field_value LONGTEXT'); db_query('ALTER TABLE field_revision_sample_text_field CHANGE sample_text_field_value sample_text_field_value LONGTEXT'); db_query("UPDATE field_config SET type = 'text_long' WHERE field_name = 'sample_text_field' ");

$field = array(
  'field_name' => 'sample_text_field',
  'type' => 'text_long',
  'cardinality' => 1,
  'settings' => array(
     'max_length' => 0,
  ),
);
field_update_field($field);    

} }

share|improve this answer

Although I think Dylan's solution is the cleanest (provided you erase the hack after updating the field), you can also just delete all content that uses the field. There are multiple ways of doing this.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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