Re: [sqlite] GUID in place of AUTO_INCREMENT

2006-01-25 Thread Mike Ashmore

On Jan 25, 2006, at 10:00 AM, Kurt Welgehausen wrote:


... but Rails doesn't seem to support composite keys.


I don't know much about RoR. How does RoR keep you from creating
a multi-column key on an SQLite table?

Regards


In point of fact, it doesn't prevent me from *creating* a multi- 
column key, just from using it effectively. It's just that Rails'  
implementation of ActiveRecord doesn't understand multi-column PKs.  
As you might suspect, ActiveRecord handles the relationships between  
records, and that's a lot easier when PKs and FKs are single-column  
and follow a reasonable naming convention.


At any rate, Dr. Hipp's suggestion of the AFTER INSERT trigger was  
exactly what I needed. Thanks!


-Mike Ashmore


Re: [sqlite] GUID in place of AUTO_INCREMENT

2006-01-25 Thread drh
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]>



Re: [sqlite] GUID in place of AUTO_INCREMENT

2006-01-25 Thread Kurt Welgehausen
> ... but Rails doesn't seem to support composite keys.

I don't know much about RoR. How does RoR keep you from creating
a multi-column key on an SQLite table?

Regards