5

All,

Is there any way to add a unique constraint to a Postgresql View?

CREATE OR REPLACE VIEW export AS
 (SELECT nextval('export_view'::regclass)::integer AS pid, getdata.id, getdata.service, getdata.title, getdata.date_taken, getdata.date_inserted, getdata.url, 
    regexp_replace(getdata.description, E'[\\n\\r]+', ' ', 'g') AS description, getdata.geom
   FROM getdata)

I have tried every combination to get this thing working like a regualr table but it is still not working as needed.

Thanks, Adam

1
  • Well technically, pid and id are both unique but I suppose pid will always be universally unique. pastebin.com/1sznU09i <= I am trying to load a view in to a desktop application and it doesn't like anything I try.
    – aeupinhere
    Dec 6, 2011 at 19:08

2 Answers 2

6

You could write a function to check to integrity of the data selected by the view. Then you would add an after-trigger to any table that is used within the view to call your function. Whenever the self-made integrity constraint checker function sees a violation, your trigger could raise an exception to abort the transaction.

Having this trigger construct should simulate constraints on views but is also pretty fragile. Unfortunately there does not seem to be native facility to add constraints to views.

0

No views are wrappers over tables. You have to put constraint on the table.

1
  • Looks like from your pastebin you just need a unique id included on your view output. What if you added a rownum just to see if you software will accept that. For fun create the view with just select 1,'XYZ' UNION select 2,'ABC'; Does your app like that?
    – Kuberchaun
    Dec 6, 2011 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.