Sam
Sam

Reputation: 21

Unknown column in 'field list' errno 1054 caused by Trigger

I have 2 parent tables (Food, Orders) with one mutual child table (OrderItems)

When using a simple insert query: INSERT INTO OrderItems (order_id, food_id, quantity) VALUES (111, 18, 2)

I get the known error: Unknown column in 'field list'

I've pinpointed the reason to a trigger that calculates a queue_time according to food_id * quantity for multiple rows:

BEGIN
    UPDATE Orders
    SET queue_time = (
        SELECT SUM(Food.food_time * OrderItems.quantity)
        FROM Food
        WHERE Food.food_id = NEW.food_id
    )
    WHERE Orders.order_id = NEW.order_id;
END

The problem lies somewhere at "OrderItems.quantity", since the error does not occur when I replace it by for example 10.


I've also tried using this Trigger:

BEGIN
    DECLARE total_queue_time INT;
    
    SELECT SUM(Food.food_time * NEW.quantity)
    INTO total_queue_time
    FROM Food
    WHERE Food.food_id = NEW.food_id;

    UPDATE Orders
    SET queue_time = total_queue_time
    WHERE Orders.order_id = NEW.order_id;
END;

But then the calculation only works for 1 row.

Upvotes: 0

Views: 179

Answers (0)

Related Questions