Reputation: 1997
I'm drawing up some class diagrams here. I am wondering should I include auto-incremented attributes like the primary key user_id in the User table? Should I include these primary/foreign key id attributes in my classes? It certainly seems like I should, eg., orderId is a critical identifier for orders in the Order table, even though it is 'only' an arbitrary auto-incremented integer...
Upvotes: 5
Views: 6867
Reputation: 6086
Class diagrams can be utilized for different purposes, at different levels. Three common usages are design, analysis and data modeling.
1- Analysis class diagrams show the entities and their relationships and model your problem domain (no solution or technology involved)
2- Design class diagrams model your solution and may contain fabric classes. They are very close to your code.
3- Data modeling class diagrams are like entity relationship diagrams (ERD). They are used to model the database tables; they contain primary keys, foreign keys and so on.
So if you are using a class diagram for modeling your database, the answer is yes, otherwise no.
The only reason you would have a primary key field in a class is when your class is a kind of Object Relational Mapper. In that case yes, include the id attribute.
If you are using Enterprise Architect, it has a special class diagram template for data modeling with all the required stereotypes like primary and foreign keys.
Upvotes: 5