PostgreSQL触发异常 [英] PostgreSQL Trigger Exception

查看:197
本文介绍了PostgreSQL触发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PostgreSQL 8.4中创建此触发器时遇到问题.

I'm having problems with creating this trigger in PostgreSQL 8.4.

CREATE OR REPLACE FUNCTION tbi_Usuarios() RETURNS TRIGGER AS $tbi_Usuarios$
    BEGIN
        IF trim(both ' ' from NEW.Nombre_usuario) = '' OR NEW.Nombre_usuario IS NULL THEN
            RAISE EXCEPTION 'Debes ingresar un nombre de usuario.';
        END IF;

    IF NEW.Password = '' OR NEW.Password IS NULL THEN
        RAISE EXCEPTION 'Debes ingresar una contraseña correctamente';
    ELSE
        NEW.Password := md5(NEW.Password);
    END IF;

    IF Fecha_registro IS NULL THEN
        NEW.Fecha_registro := current_timestamp;
    END IF;

    RETURN NEW;
END;
$tbi_Usuarios$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS tr_tbi_Usuarios ON "Usuarios";
CREATE TRIGGER tr_tbi_Usuarios BEFORE INSERT ON "Usuarios"
FOR EACH ROW EXECUTE PROCEDURE tbi_Usuarios();

问题是,当我尝试在数据库中插入一行时,出现以下错误:

The thing is that, when I try to insert a row in the database, the following error shows up:

"el registro << new >> no tiene un campo << nombre_usuario >>"

或英语:

"the table << new >> doesn't have a column << nombre_usuario >>"

但是在我的数据库中,我确实确定列Nombre_usuarioPasswordFecha_registro存在!

But in my database, I'm REALLY sure that the columns Nombre_usuario, Password, Fecha_registro exist!

有人可以帮我吗?

推荐答案

您很可能绊倒了大写字母的名称.我不厌倦建议不要使用它们.

You most probably tripped over upper case names. I am not getting tired of advising not to use those.

您可能有一个名为

"Nombre_usuario"

用双引号("")括起来,保留混合大小写的拼写.
但是在触发函数中,您需要编写:

enclosed in double-quotes ("") which preserve the mixed case spelling.
But in your trigger function you write:

NEW.Nombre_usuario

不带双引号.未加引号的标识符将转换为小写.因此,这与:

without double quotes. Unquoted identifiers are converted to lower case. So this is the same as:

NEW.nombre_usuario

但不是:

NEW."Nombre_usuario"

始终双引号混合大小写标识符.或者(更好)专门使用小写字母标识符开头,从而避免麻烦.

Always double-quote mixed case identifiers. Or (much better) exclusively use lower-case identifiers to begin with and save yourself the trouble.

首先阅读标识符和关键字" .

这篇关于PostgreSQL触发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆