Posts Tagged ‘primary key’
June 22nd, 2011
If you use Codesmith/Nettiers (recommended) you might have stumbled upon:
You set up the Nettiers project in Codesmith and mark all tables.
When you generate the code one or more entities are missing. There is no error message.
Besides the entity/entities not showing up you see it in the SourceTables property of nettiers. When you started the template all were marked. When the template was finished one/more table/s were not marked any more.
Reason: the tables are missing a primary key.
I have spent hours on this before. I did it again. Hopefully this post will make me, and others, spend less time on this issue.
September 21st, 2007
We are used to having PKs in databases as integers.
The reason for this is that computers are good at numbers and an integer is a very versatile and fast number representation.
But there are drawbacks in the business layer. A customerID can be compared to a orderID like so:
if( customerID == orderID ){…
This is not a good thing.
CustomerIDs should only be comparable to other CustomerIDs.
They should also not be addable, subtractable, multiplyable and being greater than anything else. A CustomerID is equal or not equal to another CustomerID. All other comparisions are not of this world.
I have yet to figure out how to implement this.
A comment of what to use as a primary key is found here.
June 17th, 2007
Anything simple.
Anything simple but not something the user knows about.
A 32 bit integer, a GUID, a string. But not the CustomerNumber or PostalCode or EmailAddress. No matter what the user/customer/client says – those things are not unique. I have been harrassing customers whether the CustomerNumber is unique or not. It has been unique for two weeks until someone remembered about foreign offices with their own numbering system or that they sometimes are changed.
Alas – do not use anything you find in the business logic as primary key.
A more technical comment on why we use int is found here.