AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • Início
  • system&network
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • Início
  • system&network
    • Recentes
    • Highest score
    • tags
  • Ubuntu
    • Recentes
    • Highest score
    • tags
  • Unix
    • Recentes
    • tags
  • DBA
    • Recentes
    • tags
  • Computer
    • Recentes
    • tags
  • Coding
    • Recentes
    • tags
Início / coding / Perguntas / 76985048
Accepted
andreibaboi
andreibaboi
Asked: 2023-08-27 06:15:24 +0800 CST2023-08-27 06:15:24 +0800 CST 2023-08-27 06:15:24 +0800 CST

Os gatilhos não funcionam conforme o esperado para colunas SET no mariadb

  • 772

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

mariadb
  • 2 2 respostas
  • 41 Views

2 respostas

  • Voted
  1. Best Answer
    danblack
    2023-08-27T12:32:17+08:002023-08-27T12:32:17+08:00

    Com base nas informações fornecidas, isso é descrito como um bug MDEV-32022

    • 2
  2. nbk
    2023-08-27T14:48:51+08:002023-08-27T14:48:51+08:00

    Está fora do escopo do Objeto NEW, pois você só pode acessar estritamente as colunas que estão na FROMcláusula e o que está implementado também variáveis.

    Então, você precisa, pelo menos no MariaDB, tipo:

    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 CROSS JOIN ( SELECT NEW.available_methods) n ON FIND_IN_SET(m.method, NEW.available_methods) > 0;
    END
    

    que tem o mesmo efeito.

    Você pode ver que funciona aqui .

    • 1

relate perguntas

Sidebar

Stats

  • Perguntas 205573
  • respostas 270741
  • best respostas 135370
  • utilizador 68524
  • Highest score
  • respostas
  • Marko Smith

    destaque o código em HTML usando <font color="#xxx">

    • 2 respostas
  • Marko Smith

    Por que a resolução de sobrecarga prefere std::nullptr_t a uma classe ao passar {}?

    • 1 respostas
  • Marko Smith

    Você pode usar uma lista de inicialização com chaves como argumento de modelo (padrão)?

    • 2 respostas
  • Marko Smith

    Por que as compreensões de lista criam uma função internamente?

    • 1 respostas
  • Marko Smith

    Estou tentando fazer o jogo pacman usando apenas o módulo Turtle Random e Math

    • 1 respostas
  • Marko Smith

    java.lang.NoSuchMethodError: 'void org.openqa.selenium.remote.http.ClientConfig.<init>(java.net.URI, java.time.Duration, java.time.Duratio

    • 3 respostas
  • Marko Smith

    Por que 'char -> int' é promoção, mas 'char -> short' é conversão (mas não promoção)?

    • 4 respostas
  • Marko Smith

    Por que o construtor de uma variável global não é chamado em uma biblioteca?

    • 1 respostas
  • Marko Smith

    Comportamento inconsistente de std::common_reference_with em tuplas. Qual é correto?

    • 1 respostas
  • Marko Smith

    Somente operações bit a bit para std::byte em C++ 17?

    • 1 respostas
  • Martin Hope
    fbrereto Por que a resolução de sobrecarga prefere std::nullptr_t a uma classe ao passar {}? 2023-12-21 00:31:04 +0800 CST
  • Martin Hope
    比尔盖子 Você pode usar uma lista de inicialização com chaves como argumento de modelo (padrão)? 2023-12-17 10:02:06 +0800 CST
  • Martin Hope
    Amir reza Riahi Por que as compreensões de lista criam uma função internamente? 2023-11-16 20:53:19 +0800 CST
  • Martin Hope
    Michael A formato fmt %H:%M:%S sem decimais 2023-11-11 01:13:05 +0800 CST
  • Martin Hope
    God I Hate Python std::views::filter do C++20 não filtrando a visualização corretamente 2023-08-27 18:40:35 +0800 CST
  • Martin Hope
    LiDa Cute Por que 'char -> int' é promoção, mas 'char -> short' é conversão (mas não promoção)? 2023-08-24 20:46:59 +0800 CST
  • Martin Hope
    jabaa Por que o construtor de uma variável global não é chamado em uma biblioteca? 2023-08-18 07:15:20 +0800 CST
  • Martin Hope
    Panagiotis Syskakis Comportamento inconsistente de std::common_reference_with em tuplas. Qual é correto? 2023-08-17 21:24:06 +0800 CST
  • Martin Hope
    Alex Guteniev Por que os compiladores perdem a vetorização aqui? 2023-08-17 18:58:07 +0800 CST
  • Martin Hope
    wimalopaan Somente operações bit a bit para std::byte em C++ 17? 2023-08-17 17:13:58 +0800 CST

Hot tag

python javascript c++ c# java typescript sql reactjs html

Explore

  • Início
  • Perguntas
    • Recentes
    • Highest score
  • tag
  • help

Footer

AskOverflow.Dev

About Us

  • About Us
  • Contact Us

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve