Tuesday, September 14, 2010

nuBuilder Primary Keys

Some have wondered why nuBuilder uses the Primary Key method it does (using php's uniqid() to create a string) and not Automatically Incremented Integers.

Here are some reasons:

  • If you rely on the database to tell you what the ID is going to be, you can never populate foreign keys on a child table without first creating the parent record. (generally that won't matter, but Murphy's Law will come up with a time when it does)
  • It is hard to create a table like contacts from two tables like employee and customer. Because both are likely to have the same Primary Keys (both probably have Primay Keys 1 and 2)
  • Creating a Primary Key from two other Primary Keys is harder with numbers. (123 and 45 gives the same result as 12 and 345)
  • When you use AutoIDs you need to go back to the database to get an answer you could have otherwise given it.
Essentially the more you automate a database, the less control a programmer has over it.