Plpgsql ERROR: missing FROM-clause entry for table when trying to insert
<p>Every time I try to insert something in table SaleosOrderDetail I get the following error:</p>

<blockquote>
  <p>ERROR:  missing FROM-clause entry for table "salesorderdetail" LINE 1:
  SELECT CustomerID FROM SalesOrderHeader WHERE SalesOrderDeta...
                                                         QUERY:  SELECT CustomerID FROM SalesOrderHeader WHERE SalesOrderDetail.SalesOrderID =
  SalesOrderHeader.SalesOrderID CONTEXT:  PL/pgSQL function
  new_order_detail() line 6 at SQL statement</p>
  
  <p>********** Error **********</p>
  
  <p>ERROR: missing FROM-clause entry for table "salesorderdetail" SQL
  state: 42P01 Context: PL/pgSQL function new_order_detail() line 6 at
  SQL statement</p>
</blockquote>

<p>The code is:</p>

<pre><code>CREATE OR REPLACE FUNCTION new_order_detail()
RETURNS trigger AS
$BODY$
DECLARE CustID INT;
BEGIN


    SELECT CustomerID FROM SalesOrderHeader WHERE SalesOrderDetail.SalesOrderID = SalesOrderHeader.SalesOrderID INTO custID;
    UPDATE Customer SET number_of_items = number_of_items + 1 WHERE CustomerID = custID ;



END;
$BODY$ LANGUAGE plpgsql;


DROP TRIGGER IF EXISTS new_order ON SalesOrderDetail;

CREATE TRIGGER new_order
    AFTER INSERT OR UPDATE ON SalesOrderDetail
    FOR EACH ROW EXECUTE PROCEDURE new_order_detail();
</code></pre>