Welcome Guest, Not a member yet? Register   Sign In
Why I use UUIDs for primary/foreign keys
#40

[eluser]dcheslow[/eluser]
I'm not sure that I'd create a polymorphic relation like that at all. As pointed out in an earlier post, UUIDs *could* be used without any indication of the table they refer to... BUT, retrieving a related record that way would be a nightmare. I use it for my metadata table (createdat, createdby, etc.) because I always JOIN the other way. I've never had a desire to "show me all the records created in the last hour" or anything like that.

When I do want a polymorphic relationship... for example, when doing a Search, I perform a UNION and SELECT the table name... like this:

SELECT
'page' as table,
pageid as id,
title,
....
FROM
...
WHERE
....
UNION
SELECT
'resource' as table,
resourceid as id,
title,
....
FROM
...
WHERE
....

This is no better (or worse) than using integers... except that UUIDs guarantee that I can't accidentally join to the wrong record/table.

I forgot to mention another benefit of using UUIDs in my original post. Well, I mentioned it but did not explain it well. They are disposable.

We all know the edit cycle: fill the fields, validate, if not valid then re-edit otherwise insert/update. This gets tricky when we edit multiple related records... which is a really cool ability using javascript. The reason it gets tricky is that, if we use any sort of autoincrement field, a new record has no ID until it is saved... but the user has not successfully saved the main record yet. We can't save related records until we save the main record because there is no foreign key to relate to. If we use UUIDs as keys, then we can assign an ID to an UNsaved new record and then do whatever we want with related records. If the user does not save the main record, then no harm done. TRUE... this sometimes results in orphaned (related) records, but... From a user's point of view, which as the better system... one that occasionally clears away orphaned records in the background or one that makes the user upload a file AGAIN just because they fat-fingered their phone number? I was really surprised at how many times I used the construct
Code:
if($primarykey == '')
before I switched to UUIDs.

As for UUIDs letting developers be lazy... I'm all for it! All the time I do NOT spend debugging a bad foreign key reference is time I can spend doing something else more productive. UUIDs haven't stopped me from writing bad foreign key references, they just make it easier for me to find them.

Anyway... I'm glad my post made a few people think about UUIDs. Personally, I've done it both ways and (for me) UUIDs are better.

=dave=


Messages In This Thread
Why I use UUIDs for primary/foreign keys - by El Forum - 06-06-2009, 03:56 PM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-06-2009, 04:10 PM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-06-2009, 04:28 PM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-06-2009, 05:01 PM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-06-2009, 07:02 PM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-06-2009, 07:44 PM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-07-2009, 07:06 AM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-07-2009, 07:11 AM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-07-2009, 07:32 AM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-07-2009, 09:11 AM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-07-2009, 09:59 AM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-07-2009, 10:34 AM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-07-2009, 10:49 AM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-08-2009, 12:47 AM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-08-2009, 01:47 AM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-08-2009, 02:03 AM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-08-2009, 02:24 AM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-08-2009, 07:41 AM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-08-2009, 07:45 AM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-08-2009, 07:52 AM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-08-2009, 08:08 AM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-08-2009, 08:12 AM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-08-2009, 08:15 AM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-08-2009, 08:21 AM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-08-2009, 08:24 AM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-08-2009, 08:26 AM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-08-2009, 08:28 AM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-08-2009, 08:30 AM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-08-2009, 08:33 AM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-08-2009, 08:35 AM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-08-2009, 08:39 AM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-08-2009, 08:39 AM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-08-2009, 08:46 AM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-08-2009, 08:52 AM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-08-2009, 08:53 AM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-08-2009, 08:59 AM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-08-2009, 09:01 AM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-08-2009, 09:04 AM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-08-2009, 09:12 AM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-08-2009, 06:08 PM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-08-2009, 09:44 PM
Why I use UUIDs for primary/foreign keys - by El Forum - 06-08-2009, 09:49 PM



Theme © iAndrew 2016 - Forum software by © MyBB