|
Home > Archive > PHP with PostgreSQL > June 2005 > varchar error
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Raul Secan 2005-06-23, 3:24 am |
| 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.
| |
| Volkan YAZICI 2005-06-23, 3:24 am |
| Hi,
On 6/23/05, Raul Secan <raul@zerosoft.ro> wrote:
> 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 think that it's the function of database to manipulate the input.
> 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?
IMHO, you can create an insert (and update) procedure (like
my_insert() and my_update()) with using substr() [1] function.
[1] www.postgresql.org/docs/8.0/interac...ons-string.html
Regards.
---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster
| |
| Anthony van den Berg 2005-06-23, 3:24 am |
| you might try a trigger!
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.
--
Kaartjeposten.nl
Molukkenstraat 80 / 7512 XT Enschede
* info@kaartjeposten.nl / msn. kaartjeposten@hotmai
l.com
i. www.kaartjeposten.nl / t. 06 - 28 13 88 57
Nu ook voor uw verjaardagkalenders
| |
| Marco Colombo 2005-06-23, 7:24 am |
| 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('alongstr
ing' 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('alongstr
ing' 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
---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster
| |
| Bruno Wolff III 2005-06-23, 11:24 am |
| On Thu, Jun 23, 2005 at 11:18:45 +0300,
Raul Secan <raul@zerosoft.ro> 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?
The way to do this is to use the substring function to do this. You can extract
the first 5 characters of the string you are supplied.
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?
http://archives.postgresql.org
|
|
|
|
|