How to model a many-to-many relationship where each relation has additional "relation data"?

63 views
Skip to first unread message

Jonas L

unread,
Feb 19, 2014, 7:39:34 AM2/19/14
to rav...@googlegroups.com

Hi,

I'm trying to get the grip of the document structure instead of the relational one.

In this case, I have companies and users with a many-to-many relation between them.

public class User {
   
public string Id { get; set; }
   
public string Name { get; set; }
   
public string Email { get; set; }
}

public class Company {
   
public string Id { get; set; }
   
public string Name { get; set; }
}


For each user a company has, a license is required and assigned on a "day-to-day" basis. The company can lock the users as well so that they cannot login. I guess I would need something like this inside each company:

"Users": [
   
{
       
"Id": "users/1",
       
"LicenseValidUntil": "2014-02-28T....Z",
       
"Locked": false
   
},
   
{
       
"Id": "users/4",
       
"LicenseValidUntil": "2014-03-10T....Z",
       
"Locked": false
   
}
]


Each user can be in multiple companies so ValidUntil and Locked is not specific to the user, but the user-company combination. I'm not quite sure how to model this. It doesn't feel right to create a denormalized reference with properties not found on the user entity - or is it?

Suggestions are welcome. Thanks!

Oren Eini (Ayende Rahien)

unread,
Feb 19, 2014, 8:54:07 AM2/19/14
to ravendb
You would have a CompanyUser collection in the company that contains that information.

 RavenDB Conference

Oren Eini
CEO
Hibernating Rhinos
Office:    +972-4-674-7811
Fax:       +972-153-4622-7811





--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Kijana Woodard

unread,
Feb 19, 2014, 9:51:48 AM2/19/14
to rav...@googlegroups.com

Yes. Model the relationship explicitly as a document or part of a doc, as Oren suggested, rather than implying on either side.

Jonas L

unread,
Feb 19, 2014, 3:06:10 PM2/19/14
to rav...@googlegroups.com
Thank you! And to get all companies a user is in, I would use an index?

Kijana Woodard

unread,
Feb 19, 2014, 4:30:32 PM2/19/14
to rav...@googlegroups.com
Yes. Or maintain list that on the User doc.

Assuming Company and User docs are in the same db, you can do that transactionally when handling to the Add/RemoveCompany command(s).
Reply all
Reply to author
Forward
0 new messages