pg.ddx.io  pgsql-php@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Marco Colombo <pgsql@esiway.net>
To: Raul Secan <raul@zerosoft.ro>
Cc: pgsql-php@postgresql.org
Subject: Re: varchar error
Date: Thu, 23 Jun 2005 13:29:07 +0200
Message-ID: <1119526147.24016.208.camel@Frodo.esi> (raw)
In-Reply-To: <000d01c577cc$2c9c1f30$0a10a8c0@zerosoft.ro>
References: <000d01c577cc$2c9c1f30$0a10a8c0@zerosoft.ro>

On Thu, 2005-06-23 at 11:18 +0300, Raul Secan wrote:
> Hello, I just have this:
>  
> CREATE TABLE test (
>     mytext varchar(5)
> ) WITHOUT OIDS;
>  
> If I put a string with more than 5 chars in mytext, I receive an
> error, regarding the wrong lenght of the string.
>  
> In MySQL I know that the string is automatically reduced to the number
> of char allowed by the column, even if I insert a longer string.
>  
> I don't want to do this from PHP, and I was wandering how this can be
> done in PostreSQL? Maybe in CREATE TABLE definition?
>  
> Cheers, Raul.

The job of the database is to accept valid data and to refuse invalid
ones, not to silently convert invalid data into a valid form.

While it is possible to do that conversion in PostgreSQL, I suggest you
either reconsider doing it in the application (the place it belongs to),
or think again about the schema (maybe storing the whole string).

BTW, you can also truncate the string at insert time, just change:

INSERT INTO test (mytext) VALUES ('alongstring');

into:

INSERT INTO test (mytext) VALUES (substring('alongstring' for 5));

Here it is, in action:
marco=# CREATE TABLE test (mytext varchar(5)) WITHOUT OIDS;
CREATE TABLE
marco=# INSERT INTO test (mytext) VALUES ('alongstring');
ERROR:  value too long for type character varying(5)
marco=# INSERT INTO test (mytext) VALUES (substring('alongstring' for 5));
INSERT 0 1
marco=# SELECT * FROM test;
 mytext
--------
 along
(1 row)

Of course, you have to do that on every UPDATE, too.

If that's what you want to achieve, I find it much more readable to do
the substring() or the PHP equivalent explicitly, rather than relying on
some implicit RULE or TRIGGER (or worse, on a database that silently
truncates it).

For sure I get puzzled when SELECT returns 'along' after I do INSERT
'alongstring'. Think about consistency.

.TM.
-- 
      ____/  ____/   /
     /      /       /                   Marco Colombo
    ___/  ___  /   /                  Technical Manager
   /          /   /                      ESI s.r.l.
 _____/ _____/  _/                      Colombo@ESI.it




view thread (6+ messages)  latest in thread

Message-ID: <1119526147.24016.208.camel@Frodo.esi>
Permalink:  ../1119526147.24016.208.camel@Frodo.esi/
Also on:    postgresql.org/message-id/1119526147.24016.208.camel@Frodo.esi

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-php@postgresql.org
  Cc: pgsql@esiway.net, raul@zerosoft.ro
  Subject: Re: varchar error
  In-Reply-To: <1119526147.24016.208.camel@Frodo.esi>
  References: <000d01c577cc$2c9c1f30$0a10a8c0@zerosoft.ro> <1119526147.24016.208.camel@Frodo.esi>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox