Question

I cannot seem to track down why MySQL is throwing an error when trying to update a column in a table given the following.

The update query is:

update jobitems set itemprice=itemprice/100;

The error is:

 Error Code: 1054. Unknown column 'JobID' in 'where clause'

When I remove the following from the triggers on jobitems, the update works.

DELIMITER $$
CREATE
DEFINER=`root`@`localhost`
TRIGGER `alacarte`.`jobitems_beforeupdate`
BEFORE UPDATE ON `alacarte`.`jobitems`
FOR EACH ROW
Begin
declare customer varchar(45);
Set @customer = (select CustomerID FROM jobs WHERE JobID=old.JobID);
If new.ItemCompleted<=>1
AND (select InvoiceStatus
    FROM 
        (Select InvoiceStatus, CustomerID
        FROM invoices
        as custinvoices
        Where CustomerID=@customer)
    as InvoiceStatus
    WHERE InvoiceStatus='Open') IS NULL
then
insert into invoices
set
CustomerID=@customer,
InvoiceBillToName=ifnull((select CustomerName FROM customers WHERE CustomerID=@customer),'-'),
InvoiceBillToAddress=etc;

Naturally, the jobs table has a JobID and CustomerID column. Many other triggers and our front end utilize these columns and they work flawlessly.

The following query works and returns "cust-000002" which is correct.

Set @customer = (select CustomerID FROM jobs WHERE JobID='ALC-20121119-001');
select @customer;

Also the following query works (ItemCompleted=0 for this jobitem, so it is not triggering the above trigger):

update jobitems set itemprice=itemprice/100 where JobID='ALC-20121119-001';

So, I am officially stuck with the above info. Please help :).

Update:

Eliminating the variable and replacing "@customer" with (select CustomerID FROM jobs WHERE JobID=old.JobID) throughout the trigger give the same error, as was expected.

Also, by modifying the trigger, the error goes away. I used the variable in simply a new way and it works. The following adds the output of @customer to each job into a test column and works perfectly. Err!

declare customer varchar(45);
Set @customer = (select CustomerID FROM jobs WHERE JobID=old.JobID);

If new.ItemCompleted<=>1
then
update jobs set testcol=@customer where JobID=old.JobID;
end if;

Update:

Just to make sure I am not crazy, I copy and pasted the trigger back and I get the exact same error message as before so this problem is reproducable. Also, just to be thorough, entries with "ItemCompleted=1" cannot be updated without error and entries with "ItemCompleted=0" can be updated without error found from trying to edit entries via our front end one by one. I figured I'd just make sure because I am thouroughly confused right now.

Was it helpful?

Solution

I would try qualifying JobID by calling it jobs.JobID wherever it is used, for additional disambuguation.

But I my dba instinct suggests that the actual problem may be somewhere else -- you may have another trigger that's getting fired off by something that's happening in this trigger... perhaps a trigger on the jobs or invoices tables? Or in an AFTER UPDATE on this same table?

Turn on your general log with SET GLOBAL general_log = 1 and then run the query that causes the error in the trigger.

Then turn off the general log again (so the error is easier to find, and so don't fill your hard drive), and go look in the general log at exactly what was really happening when you hit the error... the general log should actually log the queries that get run from inside the triggers, in addition to the queries you manually ran that initially caused the triggers to fire.

OTHER TIPS

When you do:

(select CustomerID FROM jobs WHERE JobID=old.JobID)

JobID, without a prefix, is the column from the jobs table, which you say you have. Now old.JobId is jobitems.JobID, since old references the table used in the update that triggers this.

Check that the column name you are matching from jobitems is exactly jobID.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top