[Pgsql-ayuda] default a una función

Gunnar Wolf gwolf@gwolf.cx
Fri, 22 Aug 2003 19:40:28 -0500


Hola,

Me topé hoy con un problema: Tengo una tabla que relaciona a otras dos
indicando un órden específico:

|    Column     |  Type   | Modifiers 
|---------------+---------+-----------
| person_id     | integer | not null
| proposal_id   | integer | not null
| mention_order | integer | not null

Creé la siguiente función:

|CREATE OR REPLACE FUNCTION next_author_mention_order(integer) returns
|INTEGER AS
|'DECLARE
|        prop_id ALIAS FOR $1;
|        verif_prop_id INTEGER;
|        highest_m_o INTEGER;
|BEGIN
|        -- Check that the proposal exists
|        SELECT INTO verif_prop_id id FROM proposal WHERE id = prop_id;
|        IF NOT FOUND THEN
|                RETURN 0;
|        END IF;
|        SELECT INTO highest_m_o mention_order FROM authors WHERE proposal_id = prop_id ORDER BY mention_order DESC;
|        IF NOT FOUND THEN
|                -- No authors have been registered for this proposal yet.
|                -- Next available number is 1.
|                RETURN 1;
|        END IF;
|        return highest_m_o + 1;
|END;'
|LANGUAGE 'plpgsql';

Quisiera que el valor de salida de esta función fuera el valor default
para nuevas inserciones, de modo que pudiera hacerlas pasando sólo el
person_id y el proposal_id. Sin embargo, claro está:

|ALTER TABLE authors ALTER COLUMN mention_order SET DEFAULT next_author_mention_order(proposal_id);
|ERROR:  cannot use column references in DEFAULT clause

¿Hay algún modo de lograr lo que estoy buscando?

Saludos,

-- 
Gunnar Wolf - gwolf@gwolf.cx - (+52-55)5630-9700 ext. 1366
PGP key 1024D/8BB527AF 2001-10-23
Fingerprint: 0C79 D2D1 2C4E 9CE4 5973  F800 D80E F35A 8BB5 27AF