Jump to content

Restricting many-to-many relationships


MmmVomit

Recommended Posts

I've got a basic many-to-many relationsip set up in my database

 

RECORD
-------------
ID
RecordData


TYPE
-------------
ID
TypeDescription


RECORD_TYPE_JUNCTION
--------------
ID
RecordID  => RECORD.ID
TypeID    => TYPE.ID

 

What I'd like to do is restrict the specific combinations of types are allowed.  In general each RECORD will only get one TYPE, but there are certain combinations of types that I need to make possible.

 

I've come up with a solution by adding a GROUP table.

 

GROUP
-----------------
ID
Group
TypeID  => TYPE.ID

 

The data in this table would look something like this.

 

GROUP
-----------------
ID   Group   TypeID
1     1       1
2     1       4
3     1       5
4     2       2
5     2       4
6     3       2
7     3       5
8     3       7

 

A valid combination of types would be (1, 4, 5), (2, 4), or (2, 5, 7).  If a type does not appear in the GROUP table, it is a singleton and is not allowed in combination with any other types, e.g. 6.  Any subset of a valid group is allowed, so (4, 5) is allowed, because it is a subset of group 1.

 

Does anyone see drawbacks to this?

Is there a better way of doing this?

Link to comment
https://forums.phpfreaks.com/topic/72861-restricting-many-to-many-relationships/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.