PostgreSQL Trigger Exception

旧城冷巷雨未停 提交于 2020-01-06 14:13:44

问题


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 >>"

or in English:

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

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

Can anyone help me please?


回答1:


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

You probably have a column named

"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

but not:

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.

Start by reading the chapter "Identifiers and Key Words" in the manual.



来源:https://stackoverflow.com/questions/9831377/postgresql-trigger-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!