Re: Changing ids conflicting with serial values?

From: Jerry Sievers <jerry(at)jerrysievers(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Changing ids conflicting with serial values?
Date: 2005-11-04 14:06:58
Message-ID: m3hdas4id9.fsf@prod01.jerrysievers.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Steven Brown <swbrown(at)ucsd(dot)edu> writes:

> I'm granting access to insert/update/delete rows of a table to people,
> but I don't want all future inserts to fail if they decided to change an
> id (which they obviously shouldn't, but they /can/). It makes for a
> fragile system.

create rule no_pkey_update
as on update to foo where new.id != old.id
do instead nothing;

Simple minded solution with negative aspect that it will silently skip
ANY update trying to change pkey... other changes to record also
discarded.

> Should I just be using some sort of trigger to block them from modifying
> the id, or is there another way to handle it? I.e., how do people
> normally handle that? It's a migration thing - MySQL prevented this
> situation due to the way it handles auto_increment (it will never assign
> you an id that already exists).

Bit more complex but still easy is trigger to just always set new.id
to old.id thereby insuring that it can't be changed.

create function no_pkey_update()
returns trigger
as '
begin
new.id = old.id;
return new;
end'
language plpgsql;

HTH

--
-------------------------------------------------------------------------------
Jerry Sievers 305 854-3001 (home) WWW ECommerce Consultant
305 321-1144 (mobile http://www.JerrySievers.com/

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Jerry Sievers 2005-11-04 14:22:55 Re: how to emit line number in a function?
Previous Message Peter Filipov 2005-11-04 11:49:19 records to text representation