Re: Updatable view should truncate table fields

From: "David Johnston" <polobo(at)yahoo(dot)com>
To: "'Russell Keane'" <Russell(dot)Keane(at)inps(dot)co(dot)uk>, <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Updatable view should truncate table fields
Date: 2011-11-08 21:45:59
Message-ID: 007a01cc9e5f$cddf0a90$699d1fb0$@yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

From: pgsql-sql-owner(at)postgresql(dot)org [mailto:pgsql-sql-owner(at)postgresql(dot)org]
On Behalf Of Russell Keane
Sent: Tuesday, November 08, 2011 4:34 PM
To: pgsql-sql(at)postgresql(dot)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 field2
FROM 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.

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Ross Reedstrom 2011-11-08 22:59:37 Re: help with xpath namespace
Previous Message Russell Keane 2011-11-08 21:34:08 Updatable view should truncate table fields