A Report can have multiple charts. The Chart table looks as follows:
- Chart -- Id, ChartId, ReportId, ...
The ChartId above can map to the ChartId to either one of the following Chart Types:
- Line: ChartId, Thickness, YAxis, XAxis, Color, ...
- Pie: ChartId, Radius, Color, ...
- Bar: ChartId, Width, Color, Border, ...
I am using SQL Server and unfortunately alternate solutions like using CouchDB etc are not viable options. I also intend to map these tables to entities using Entity Framework.
This design is my first stab and I see multiple problems with this design:
- Chart.ReportId cannot be a foreign key on multiple tables so enforcing an FK is a problem.
- When running an SQL (Or Linq) query, I'll have to do either run two queries to get the chart type and then query that table. Or, I'll have to run a join against all the tables to get the chart data.
- When doing a join against the Chart Types, The ChartId across these tables need to be unique so Chart.ChartId maps to only one of Line.ChartId or Bar.ChartId or Pie.ChartId.
- Mapping this with EF will be tricky.
I would imagine this is a common enough problem that there are prescribed solutions and established ways to handle it.
Am I on the right path with the design? And if so, how to overcome the above problems (FK, join, unique id across multiple tables, and mapping).
Solution
This answer is quite comprehensive about the strategies. And SQL Team's article on implementing table inheritance in SQL Server got me where I wanted.