Re: Updatable view should truncate table fields - Mailing list pgsql-sql

From David Johnston
Subject Re: Updatable view should truncate table fields
Date
Msg-id 007a01cc9e5f$cddf0a90$699d1fb0$@yahoo.com
Whole thread Raw
In response to Updatable view should truncate table fields  (Russell Keane <Russell.Keane@inps.co.uk>)
List pgsql-sql
From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql-owner@postgresql.org]
On Behalf Of Russell Keane
Sent: Tuesday, November 08, 2011 4:34 PM
To: pgsql-sql@postgresql.org
Subject: [SQL] Updatable view should truncate table fields

Using PostgreSQL 9.0.

We have a table which is not accessible by client code.
We also have views with rules and triggers to intercept any insert or update
statements and write that data in a slightly different format back to the
table.

A particular field in the table is currently 5 chars but recently we have
had update / insert statements containing more than 5.
This obviously (and correctly) throws an error.

We can extend the table to accept more than 5 characters but the view must
return 5 characters.
If we try to extend the table to accept, say, 10 characters the view will
display 10.
If I also cast the view field to 5 characters then any insert with more than
5 characters still fails.

-----------------------------------------------------

Haven't used updatable VIEWs yet but couldn't you either define the VIEW as:

CREATE VIEW .... AS (SELECT  field1, field2::varchar(5) AS field2FROM table
);

Or, alternatively, define the INSERT/UPDATE functions to perform the
truncation upon inserting into the table?

Does the INSERT itself throw the error or is one of your functions raising
the error when it goes to insert the supplied value into the table?

It is generally bad form to modify user data for storage so either  you
truly have a length limitation that you need to restrict upon data entry (in
which case everything is working as expected) or you should allow any length
of data to be input and, in cases where the output medium has length
restrictions, you can ad-hoc limit the display length of whatever data was
provided.

David J.



pgsql-sql by date:

Previous
From: Russell Keane
Date:
Subject: Updatable view should truncate table fields
Next
From: Ross Reedstrom
Date:
Subject: Re: help with xpath namespace