Forum OpenACS Q&A: alter table add constraint

Collapse
Posted by Jonathan Ellis on
I want to add a field, default_party_id, to the users table, and check to make sure that the referenced party actually belongs to the user.

bf2=# alter table users add default_party_id int references parties
(party_id);

ALTER

bf2=# alter table users add constraint party_id_valid check
(user_id = (select user_id from parties where party_id = 
default_party_id))
;

bf2-# bf2-# ERROR:  ExecEvalExpr: unknown expression type 108
I'm not sure what to make of this error...
Collapse
Posted by Don Baccus on
Hmmm....maybe it doesn't allow subselects in check constraints?  I grep'd for default_party_id and couldn't find it anywhere, hmmm...
Collapse
Posted by Daryl Biberdorf on

According to the PostgreSQL docs:

Currently, CHECK expressions cannot contain sub-selects nor refer to variables other than fields of the current row.

Looks like this will probably need a trigger.

Collapse
Posted by Jonathan Ellis on
Ah... thanks, Daryl, I missed that.