You're correct, and I apologize for the confusion. In DuckDB, the DEFAULT clause does not support referencing other columns directly. This is because the DEFAULT clause is evaluated at the time of table creation, not at the time of row insertion.
To achieve the desired behavior, you'll need to use a trigger instead of a DEFAULT clause. Here's an example:
sql
CREATE TABLE mytable (
category VARCHAR,
label VARCHAR
);
CREATE TRIGGER set_label_default
BEFORE INSERT ON mytable
FOR EACH ROW
BEGIN
IF NEW.label IS NULL THEN
SET NEW.label = CASE
WHEN NEW.category = 'error' THEN 'ERR-' || NEW.category
ELSE NEW.category
END;
END IF;
END;
In this example:
We create the mytable table without a DEFAULT clause for the label column.
We create a BEFORE INSERT trigger named set_label_default that fires before each row insertion.
The trigger checks if the label column is NULL. If it is, the trigger sets the label value based on the category column using a CASE expression.
Now, when you insert data:
sql
INSERT INTO mytable (category) VALUES ('error'), ('warning'), ('info');
The label column will automatically get the computed values: