[development] Form field maxlengths vs. database field sizes

Steven Wittens steven at acko.net
Thu Feb 9 09:01:11 UTC 2006

Chris Johnson schreef:

> Adrian Rossouw wrote:
>> On 08 Feb 2006, at 6:22 AM, Karthik wrote:
>>> This issue stems from Killes' comment [
>>> http://drupal.org/node/46306#comment-71971 ]. Should form field
>>> maxlengths be consistent with corresponding database field sizes or
>>> should unicode encoding be accounted for?
>> Drupal has no way of knowing what the field size of the database is,
>> and as such the forms api can't take that into account.
> It does not presently, but Drupal could know the field sizes in the 
> database tables.  However, is that worth doing?  (I'm presently 
> developing a database abstraction layer for my employer where the db 
> layer figures out the metadata, like field sizes, on the fly so that 
> the applications need not know anything at all about it.)

The problem is inconsistency across mysql versions. On MySQL 4.1+ (and 
w/ drupal 4.7), database field sizes are counted in characters. On 4.0 
and below, they are in bytes. So for a varchar(256) we could have from 
64 to 256 characters (4 to 1 bytes per character) with UTF-8. Should we 
set the maxlength to 64 then? We settled on the compromise of half (128) 
on the assumption that most people use 1-2 byte characters.

Once we phase out MySQL 4.0 we can safely make them all match the 
database sizes.


More information about the development mailing list