How to use custom function in MariaDB computed column definition?

I’m working with MariaDB 10.6.22 on Ubuntu 24.04 and created a custom function to remove accents from text fields. The function works perfectly when I call it directly on existing data, but I’m having trouble using it in a generated column definition.

When I try to create a table like this:

CREATE TABLE user_data(
    id BIGINT NOT NULL AUTO_INCREMENT,
    full_name TEXT,
    birth_year INTEGER DEFAULT 0,
    category TEXT DEFAULT NULL,
    normalized_name TEXT GENERATED ALWAYS AS (remove_accents(full_name)) STORED,
    PRIMARY KEY(id)
);

I get this error: ERROR 1901 (HY000): Function or expression 'remove_accents()' cannot be used in the GENERATED ALWAYS AS clause

Here’s my custom function:

DELIMITER $$
CREATE OR REPLACE FUNCTION remove_accents(input_text TEXT)
RETURNS TEXT DETERMINISTIC
BEGIN
    DECLARE processed_text TEXT;
    DECLARE accent_chars TEXT;
    DECLARE normal_chars TEXT;
    DECLARE char_position INT;
    
    SET processed_text = input_text;
    SET accent_chars = 'ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïñòóôõöùúûüý';
    SET normal_chars = 'AAAAAACEEEEIIIINOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuy';
    SET char_position = LENGTH(accent_chars);
    
    WHILE char_position > 0 DO
        SET processed_text = REPLACE(processed_text, 
            SUBSTRING(accent_chars, char_position, 1), 
            SUBSTRING(normal_chars, char_position, 1));
        SET char_position = char_position - 1;
    END WHILE;
    
    RETURN processed_text;
END$$

What am I missing to make this work with generated columns?

yeah, mariadb can be a pain with that! if ur fn is deterministic, try a trigger instead for generated values. it’s a hassle, but that’s the workaround for now.

This happens because MariaDB won’t let you use custom functions in generated columns, even if they’re marked DETERMINISTIC. MariaDB maintains a whitelist of “safe” functions for generated columns, and custom user-defined functions don’t make the cut. I ran into this exact problem building a text processing system. Two ways to fix it: either chain together built-in functions like REPLACE() directly in the generated column expression, or ditch the generated column and use a BEFORE INSERT/UPDATE trigger to populate a regular column instead. The trigger approach gives you way more flexibility and sidesteps MariaDB’s restrictions completely.

hmm, that’s interesting! i’ve run into similar issues with custom functions in generated columns. is your function marked as deterministic? mariadb can be picky about that. try testing remove_accents() directly in a SELECT statement first - does it give consistent results with the same inputs?