views:

964

answers:

4

I am working on an application that will be used as an extensible framework for other applications.

One of the fundamental classes is called Node, and Nodes have Content. The SQL tables look like this:

TABLE Node ( NodeId int, .... etc )

TABLE NodeContentRelationship ( NodeId int, ContentType string, ContentId int)

It will be up to the developers extending the application to create their own content types.

Clearly this is bad from a relationship-database point of view as it is not possible to add a foreign key relationship to NodeContentRelationship.ContentId, even though it is a foreign key column.

However, the solution is quite simple and powerful so I am reluctant to change it.

What do you think - am I in for a world of pain down the track?

+6  A: 

Beware the Inner Platform Effect.

If you're trying to build an 'extensible framework' which allows developers to store data of different 'content types' and relate them to each other in a generic fashion, you may find that others have already solved this problem.

Ben McEvoy
Good call - definitely something to watch out for with this project!
cbp
Feel free to click the 'tick' next to my answer then ;)
Ben McEvoy
+1 for knowing the inner platform effect
ammoQ
-1 for generic links to "solved this problem"
Liao
Those links were intended as very tongue in cheek, Liao - apologies for omission of the sarcasm tag ;)
Ben McEvoy
+3  A: 

Why is it impossible to set NoteContentRelationship.ContentId as a foreign key? You can easily use a relational inheritance model with a table Content representing an abstract base class, and various tables AnimalContent, CarContent, etc. representing derived classes.

Justice
So by that you mean the schema would look like this:TABLE Node ( NodeId int, .... etc )TABLE AnimalContent ( AnimalContentId int, NodeId int, IsMammal ... etc)TABLE CarContent (CarContentId int, NodeId int... etc.)You're right - that would be better from a relational database point-of-view.
cbp
+3  A: 

This looks to me like a variation on EAV (Entity, Attribute, Value) design.

The benefits and drawbacks of EAV design have been extensively documented.

Here is a description of EAV from a sympathetic point of view:

http://ycmi.med.yale.edu/nadkarni/Introduction%20to%20EAV%20systems.htm

And here is one from a hostile point of view:

http://tonyandrews.blogspot.com/2004/10/otlt-and-eav-two-big-design-mistakes.html

Be aware of the downside of EAV before you commit thousands of man-hours into pouring data into it. It's enormously seductive to programmers, but it can become a data manager's nightmare.

Walter Mitty
A: 

You're in for a world of hurt down the road if you ever want to actually report on this data. You just made it much much harder to write joins and such. The lack of constraints is bad, but the extra work needed on queries is (IMHO) worse.

However, if you want other developers to be able to extend the system and store data in the database but not be able to change the database schema, there might not be a choice. In that case, the answer is to minimize how much gets stored this way. You might also speed it up slightly by replacing ContentType with a ContentTypeId defined in another table.

edebill