简体   繁体   中英

Npgsql.PostgresException: '42P01: relation “table” does not exist'

Im trying to autogenerate INSERT statements to populate a table from a given list of names and products using c# and Npgsql. Everything was working fine, so I decided to add couple of triggers that are needed for the purpose of this project. Triggers are relatively simple, but they performed flawlesly, while testing them on the sql console. Then, when I finally thought I was finished with the assignment, I tested it once again with my auto populating method in C#.I got this as an error:

Npgsql.PostgresException: '42P01: relation "buyer" does not exist'

I've decided to test it once again on console, using the exact same INSERT statement, and again, it worked as it should have from the start. After good 3-4h spent googling and finding little to no answers, I've disabled all triggers and then the c# script worked without any problems.

So, to sum things up, query works with triggers flawlesly while typed in the console, but when used in C# it can't find the table?.

CREATE OR REPLACE FUNCTION "TBP_ERA".trigger_dohvati_prvi_datum()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
BEGIN
  insert into "TBP_ERA".order(orderer,productname,productquantity) 
select buyer.id,buyer.productname,buyer.productquantity from buyer where buyer.ordercompleted=0 order by orderedat asc fetch first 1 rows only;
return null;
END;
$function$
;

And the trigger itself:

create
    trigger dohvati_datum after insert
        on
        "TBP_ERA".buyer for each row execute procedure trigger_dohvati_prvi_datum();

EDIT: C# part of the INSERT code

connection.Open();
            NpgsqlCommand command = new NpgsqlCommand("INSERT INTO \"TBP_ERA\".buyer(ID,name,productname,productquantity,ordercompleted)" +
                " VALUES(default,'" + buyer.Name + "','" + buyer.ProductName + "'," + buyer.ProductQuantity+",0);", connection);
            try
            {
                command.ExecuteNonQuery();
            }
            catch (NpgsqlException ex)
            {
                string nekej = ex.ToString();
                throw;
            }


            connection.Close();

Try to schema qualify buyer in the trigger function.

CREATE OR REPLACE FUNCTION "TBP_ERA".trigger_dohvati_prvi_datum()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
BEGIN
  insert into "TBP_ERA".order(orderer,productname,productquantity) 
select buyer.id,buyer.productname,buyer.productquantity from "TBP_ERA".buyer where buyer.ordercompleted=0 order by orderedat asc fetch first 1 rows only;
return null;
END;
$function$
;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM