views:

72

answers:

3

[pre-able]
In my www.twipler.com project I want to allow people to link Twitter accounts. This will be achieved by having them login, selecting "link account" and then logging in again. This will effectively give me UserId-1 and UserId-2. Now I want to allow the user to login with either UserId-1 or UserId-2 credentials and then retreive the credentials for the other user. (Feel free to comment, but this is not the question)

[the setup]
C# 3.5, NHibernate, TweetSharp, SQL 2005.

[question]
How do I store an "bag" of unique integers in a database, such that querying one interger will return the other associated ones.

For example, I might create a table called UserLinks and have GroupId which I calculate before inserting a new link. It would look like this

UserLinks  GroupId   UserId
              1      12312784
              1      18329139
              2      19319332
              2      14529342
              2      21031023

But this feels a bit ugly and un-NHibernate. (Update: note there can more than 2 accounts)

There's mention below of finding a new GroupId using Max(GroupId)+1, but I realise since the UserId's are unique I can use the first inserted UserId of the group as the GroupId.

+1  A: 

Actually that table looks to be about the right way of going to link (on the same level) 1..n records in a single table together.

Chris Lively
it's what I might end up doing, but finding max(GroupId)+1 seems like "work"
Dead account
What do you mean 'finding max(groupid)' ?Why don't you let the DB determine the new groupid ?Otherwise, you can calculate the new GroupId by having a look at the groupId's that already exists for the current user ?
Frederik Gheysels
I see no benefit of using groups vs a simple many to many relationship
Paul Creasey
+1  A: 

Why would this be 'un-nhibernate' ? NHibernate is the bridge between the relational model of the DB and the OO model of your business classes.

Store the data in the DB in its most efficient / correct way, like you would do normally when using a relational DB. Create your business classes so that you'll be able to solve the problem. Use NHibernate to make the bridge between those 2 paradigms (relational data model and OO).

So: Create your DB - model in the relational way. Create your domain-classes: You could create a User class, which has a property 'LinkedAccounts'. This property can be a list of Account objects. In your NHibernate mapping, you can then specify where (from which tables) NHibernate should get its data from.

Frederik Gheysels
I was hoping to add to my User class/entity a property "LinkedAccounts" which was an array of primary keys. Doing it myself seems like work which NHibernate might do for you.
Dead account
check my edit ...
Frederik Gheysels
+1  A: 

seems like you just need a typical self many to many relationship which you would implemtent as a table containing user_id_1 and user_id_2 as a composite PK, both of which are FK's to the userID of the user table.

Paul Creasey
The question then is where does user_id_3 go? Is it tied to 2 or 1? The OP says that there isn't a parent user account. Also, the sql, using this method, to pull all user accounts associated with a particular login would be a bit convuluted.
Chris Lively
@Chris, not at all, this is 100% standard many to many, user_id_3 doesn't exist, we would have 1,2 and 1,3. This isn't at all difficult to query, it's trivial.
Paul Creasey
In order to get from 3 to 2 you have to go through 1.
Chris Lively