Forum OpenACS Development: Response to Guidelines

Collapse
20: Response to Guidelines (response to 1)
Posted by Dan Wickstrom on
When porting functions and procdures, you need to watch out for the case where a function input variable name matches a column name used in an insert. This pattern as shown below is pretty common in the acs classic 4.x code.

create table acs_objects (
       object_id         integer primary key,
       object_type       varchar(100)
);


create function tst_insert (integer, varchar) returns integer as '
declare
        object_id       alias for $1;
        object_type     alias for $2;
begin
        insert into acs_objects 
                    (object_id, object_type) 
                     values
                    (object_id, object_type);

        return 0;
                               
end;' language 'plpgsql';

select tst_insert(1,'acs_object');

acspg=# i tst.sql
Dsql:tst.sql:7: NOTICE:  CREATE TABLE/PRIMARY KEY will create implicit index 'acs_objects_pkey' for table 'acs_objects'
CREATE
CREATE
psql:tst.sql:25: ERROR:  parser: parse error at or near "$1"
acspg=#

To fix this, you need to modify the input variable name so it doesn't match the column name used in the insert.