Isso não funciona, eu crio o gatilho perfeitamente, mas quando insiro algo nessa tabela, recebo # 1054 - Coluna desconhecida 'disponíveis_métodos' em 'lista de campos'
CREATE TRIGGER `add_notification_type` AFTER INSERT ON `notification_types`
FOR EACH ROW BEGIN
INSERT IGNORE INTO unit_templates (unit, idkey, method)
SELECT u.id AS unit, NEW.idkey, m.method
FROM units AS u
JOIN (
SELECT 'sms' as method
UNION ALL
SELECT 'push'
UNION ALL
SELECT 'email'
UNION ALL
SELECT 'chat'
UNION ALL
SELECT 'info'
) AS m ON FIND_IN_SET(m.method, NEW.available_methods) > 0;
END
Isso funciona:
CREATE TRIGGER `add_notification_type` AFTER INSERT ON `notification_types`
FOR EACH ROW BEGIN
DECLARE available_methods_var VARCHAR(255);
SET available_methods_var = NEW.available_methods;
INSERT IGNORE INTO unit_templates (unit, idkey, method)
SELECT u.id AS unit, NEW.idkey, m.method
FROM units AS u
JOIN (
SELECT 'sms' as method
UNION ALL
SELECT 'push'
UNION ALL
SELECT 'email'
UNION ALL
SELECT 'chat'
UNION ALL
SELECT 'info'
) AS m ON FIND_IN_SET(m.method, available_methods_var) > 0;
END
O problema aparece apenas para colunas do tipo SET/ENUM (na verdade tentei apenas SET). O estranho é que no mariadb 10.3 o problema apareceu apenas nos gatilhos AFTER UPDATE mas no AFTER INSERT estava funcionando. Após atualizar para o mariadb 11 o insert AFTER não funcionou mais e tive que declarar as variáveis antes e não usar o NEW.available_methods na consulta.
Alguém sabe por que isso acontece? Até tentei perguntar ao chatgpt mas a resposta dele foi e cito "é estranho" :)). Ele me disse que provavelmente tinha algo a ver com a versão do mariadb, mas isso não explica as coisas.
desde já, obrigado
Tentei a primeira variante primeiro e esperava que funcionasse
--ATUALIZAR--
Estas são as tabelas envolvidas:
--
-- Table structure for table `notification_types`
--
CREATE TABLE `notification_types` (
`idkey` varchar(40) NOT NULL,
`title` varchar(100) NOT NULL,
`editable` tinyint(1) NOT NULL DEFAULT 1,
`has_days` tinyint(1) NOT NULL DEFAULT 0,
`default_days` int(3) NOT NULL DEFAULT 0,
`has_sessions` tinyint(1) NOT NULL DEFAULT 0,
`default_sessions` int(3) NOT NULL DEFAULT 0,
`available_methods` set('sms','push','email','chat','info') NOT NULL,
`default_methods` set('sms','push','email','chat','info') NOT NULL,
`class` set('announcements','info','marketing','system') NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
--
-- Table structure for table `units`
--
CREATE TABLE `units` (
`id` int(11) NOT NULL,
`name` varchar(40) NOT NULL,
`active` int(1) NOT NULL DEFAULT 1,
`waiting` tinyint(1) NOT NULL DEFAULT 0,
`suspended` int(1) NOT NULL DEFAULT 0,
`deleted` tinyint(1) NOT NULL DEFAULT 0,
`deleted_time` int(20) NOT NULL DEFAULT 0,
`acl` text CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
`custom_fields` varchar(1000) NOT NULL DEFAULT '',
`code` varchar(15) DEFAULT NULL,
`settings` text NOT NULL DEFAULT '{}'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
--
-- Table structure for table `unit_templates`
--
CREATE TABLE `unit_templates` (
`unit` int(11) NOT NULL,
`idkey` varchar(40) NOT NULL,
`method` set('sms','push','email','chat','info') NOT NULL,
`template` text DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
Eu não incluí os índices
Você pode ver https://dbfiddle.uk/8IjWUeOK para aquele que funciona e https://dbfiddle.uk/sZsQou4a para aquele que não funciona
Com base nas informações fornecidas, isso é descrito como um bug MDEV-32022
Está fora do escopo do Objeto NEW, pois você só pode acessar estritamente as colunas que estão na
FROM
cláusula e o que está implementado também variáveis.Então, você precisa, pelo menos no MariaDB, tipo:
que tem o mesmo efeito.
Você pode ver que funciona aqui .