views:

223

answers:

3

I do not understand when to use the different lengths for calling unicode. I have been using types.Unicode(255) for all my columns in my postgres database such as address, name, description, etc. Is it unwise to do this?

A: 

There are a number of reasons to use proper datatypes in your databases. Performance (pdf) is the biggest concern, but there are other reasons too. For example:

  • Dates will not sort properly unless they are a date datatype (usually a unix timestamp)
  • You cannot use mathematical operators on numbers that are stored as strings
  • "true" as a string does not equal boolean true
cpharmston
+2  A: 

Not sure what you mean by "unicode(255)" - there is no such data type in PostgreSQL:

# create table q (x unicode(255));
ERROR:  type "unicode" does not exist
LINE 1: create table q (x unicode(255));
                          ^

Maybe you meant varchar(255). In this case - let me ask: what will happen if you'll need 320 character description?

Personally I prefer to use TEXT datatype - since database generally doesn't care if the string is 100 or 1000 characters long.

depesz
+1  A: 

It's worth adding to depesz's answer that there are no performance penalties for using text type rather than varchar(n) or char(n) data types, so unless you need to set a hard limit for business purposes just use text. Even then, use text with a length constraint :-)

xzilla