0

A library that I use currently has a bug and I'm trying to find a workaround for. Is it possible to to ignore a column on a PostgreSQL view insert? I have this view:

CREATE VIEW schema.auth AS
  SELECT email AS id, pass AS pass, 'onymous'::varchar AS rolname
    FROM schema.person;

The library is trying to do the following:

INSERT INTO schema.auth (id, pass, rolname) VALUES ('abc', '123', '');

The library should not be setting an empty string ('') for rolname, hence the bug.

Is there a way for me to stop PostgreSQL from throwing an error at rolname and quietly discard the empty string?

5
  • possible duplicate of Postgresql insert trigger to set value Commented Sep 19, 2015 at 22:14
  • It is my understanding that triggers don't work that way on views. Commented Sep 19, 2015 at 22:15
  • Yes. I tried adding a trigger before inserts/updates. Even then though, I'm still not sure how to remove a column. Commented Sep 19, 2015 at 22:20
  • You can replace the empty string with a value that will not cause the bug, e.g. NULL Commented Sep 19, 2015 at 22:31
  • I don't have access to the empty string value. Commented Sep 19, 2015 at 22:31

1 Answer 1

1

This is actually very trivial. What you do is simply "forget" rolname when propagating the INSERT on the view to the underlying tables:

CREATE FUNCTION fix_lib_issue() RETURNS trigger AS $$
BEGIN
  INSERT INTO schema.person (email, pass)
  VALUES NEW.id, NEW.pass;
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER tr_auth
INSTEAD OF INSERT ON schema.auth
FOR EACH ROW EXECUTE PROCEDURE fix_lib_issue();

When you query the view after the insert, the rolname will be set to onymous, following the view definition.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.