首页 > 解决方案 > “字段列表”中的未知列“created_by”-触发器

问题描述

我正在尝试创建触发器但收到错误消息。非常感谢您提供的任何指导。仅供参考-我是初学者....

CREATE DEFINER=root@localhost TRIGGER txn-trg 
before INSERT ON transactions 
FOR EACH row 
        if (created_by = (select id from users where role_id=2),type_id=1, internal_status_id=2 , status='STL')
            then set new.status='FINANCE';  
        elseif (status<>'STL',internal_status_id<>2,type_id<>1) 
            then set new.status='status';
        end if;

当我将记录保存在事务表中时,它返回错误消息为

SQL 错误 [1054] [42S22]: (conn=253) Unknown column created_byin 'field list' 。字段 created_by 是存在于事务表中的有效字段。

上一篇MySQL Trigger Check if Row Field Exists没有回答上述查询。

事务表 DDL

CREATE TABLE transactions (
  id bigint(20) NOT NULL AUTO_INCREMENT,
   internal_status_id bigint(20) NOT NULL,
  type_id bigint(20) NOT NULL,
  created_by bigint(20) NOT NULL,
  PRIMARY KEY (id),
  KEY transactions_int_status_FK (internal_status_id),
  KEY transactions_type_fk (type_id),
  KEY transactions_assigned_created_by_fk (created_by),
  CONSTRAINT transactions_assigned_created_by_fk FOREIGN KEY (created_by) REFERENCES users (id),
  CONSTRAINT transactions_type_FK FOREIGN KEY (type_id) REFERENCES transaction_types (id)
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=latin1;

用户表 DDL

CREATE TABLE users (
  id bigint(20) NOT NULL AUTO_INCREMENT,
  role_id bigint(20) NOT NULL,
  PRIMARY KEY (id),
  UNIQUE KEY user_user_name_un (user_name),
  KEY users_fk (role_id),
  CONSTRAINT user_role_fk FOREIGN KEY (role_id) REFERENCES roles (id)
) ENGINE=InnoDB AUTO_INCREMENT=142 DEFAULT CHARSET=latin1;

标签: mysql

解决方案


推荐阅读