Mike Ashmore <[EMAIL PROTECTED]> wrote:
> So, I've got what I think is a good reason to modify the behavior of
> primary keys in my (Ruby on Rails-based) application.
>
> What I need is a modifier, say 'GUID', that can be applied in place
> of (mutually exclusive with) the AUTOINCREMENT modifier [1]. On a
> field with the GUID modifier, if an INSERT statement proffers a NULL
> value for that field, an RFC4122-compliant GUID generator should be
> invoked to create a globally unique value for that field.
>
You can do this with an insert trigger. First create
a user-defined function to generate your guid. Suppose
that function is named new_guid(). Further suppose that
the column that wants the guid is called "pk". Then write
the trigger like this:
CREATE TRIGGER AFTER INSERT ON table1 WHEN new.pk IS NULL
BEGIN
UPDATE table1 SET pk=new_guid() WHERE rowid=new.rowid;
END;
--
D. Richard Hipp <[EMAIL PROTECTED]>