0

I'm trying to implement full text search and have successfully created a trigger function

CREATE FUNCTION content_vector_generate() RETURNS trigger AS $$
begin
    new.content_vector := 
        to_tsvector('simple', coalesce("title" ,'') || ' ' || coalesce("content" ,''));
    return new;
    end
$$ LANGUAGE plpgsql;
     
CREATE TRIGGER content_vector_generate BEFORE INSERT OR UPDATE
        ON "Posts" FOR EACH ROW EXECUTE PROCEDURE content_vector_generate();

but when i try, to check the function work properly by inserting / updating row

INSERT INTO public."Posts"(id,title,content)
VALUES(1,'post-title','etc-content')

i got this error instead

SQL Error [42703]: ERROR: column "title" does not exist
  Where: PL/pgSQL function content_vector_generate() line 3 at assignment

do I have wrong syntax, i've tried removed the quotation mark too

2
  • 1
    You need to reference the column in the NEW record: new.title and new.content. Additionally: string constants need to be enclosed in single quotes so your insert needs to be VALUES(1,'post-title','etc-content') Commented Feb 14, 2022 at 10:42
  • thankyou now it works properly Commented Feb 14, 2022 at 10:58

1 Answer 1

1

as @a_horse_with_no_name mention i need to reference the column new row in the function like :

CREATE OR REPLACE FUNCTION content_vector_generate() RETURNS trigger AS $$
begin
    new.content_vector := 
        to_tsvector('simple', coalesce(new."title" ,'') || ' ' || coalesce(new."content" ,''));
    return new;
    end
$$ LANGUAGE plpgsql;
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.