The EntitySpaces Community

Share and learn about the EntitySpaces Architecture.
Welcome to The EntitySpaces Community Sign in | Join | Help
in
Home Forums Photos

Many-to-Many with the Same table

Last post 05-06-2008, 5:38 AM by Sarosh. 3 replies.
Sort Posts: Previous Next
  •  05-05-2008, 7:02 PM 9184

    Many-to-Many with the Same table

    Hi!

    It this possible?

    I have to a "User" table and a "Group" table and a many-to-many resolver table "UserXGroup"

    Code:
    User Table
    ---------------
    IDUser (PK)
    FirstName
    LastName
    Login
    
    Group Table
    ----------------
    IDGroup (PK)
    Name
    
    UserXGroup Table
    ------------------------
    IDUser   (PK + FK to the User Table)
    IDGroup (PK + FK to the Group Table)
    
    This works no problem. What I want to do is to combine the User and Group table into one table
    
    UserGroup Table
    ---------------
    IDUserGroup (PK)
    FirstName (Used for User First Name and Group Name)
    LastName (Null for a Group)
    Login (Null for a Group)
    UserGroupType ('U' for User and 'G' for Group)
    
    UserXGroup Table
    ------------------------
    IDUser   (PK + FK to the UserGroup Table)
    IDGroup (PK + FK to the UserGroup Table)
    
    Will ES recognize this 'UserXGroup' table as a many-to-many resolver table?

    Thanks

    Sarosh

  •  05-05-2008, 7:12 PM 9186 in reply to 9184

    Re: Many-to-Many with the Same table

    I'm kind of confused, your UserXGroup in your new model has both IDUser and IDGroup yet your UserGroup table has neither, am I missing something?

    EntitySpaces | Twitter | BLOG | Please honor our Software License
  •  05-06-2008, 12:40 AM 9189 in reply to 9184

    Re: Many-to-Many with the Same table

    EntitySpaces will work with a self-referencing many-to-many, but it does not generate the direct many-to-many hierarchical property to the self-referencing collection, nor will it generate the Associate/Dissociate methods. It only generates the two zero-to-many properties into the associative table. So, no, 'UserXGroup' will not be recognized as a many-to-many resolver (associative) table.

    BTW, the current design sounds like a more properly normalized database than your proposed change. The fact that User and Group do not have the same fields, 'LastName' has to serve a dual purpose, two fields are un-used for Group, and the 'UserGroupType' field could make queries and saves more difficult, leads me to believe that the current design is "correct". And, you get the benefit of EntitySpaces many-to-many hierarchical properties.

    Most guides recommend taking your database to at least 3rd normal form, unless there are good, practical reasons to de-normalize (meaning it solves more problems than it creates).

    http://www.datamodel.org/NormalizationRules.html


    David Neal Parsons
    www.entityspaces.net
  •  05-06-2008, 5:38 AM 9196 in reply to 9186

    Re: Many-to-Many with the Same table

    Hi!

    In the new model the Users and Groups are both stored in the same UserGroup Table and the PK is IDUserGroup and the UserGroupType (Flag) column determines if a row is of type "U" - User or "G" - Group so that's why both the UserXGroup table's (many-many resolver) columns point to  the UserGroup table one points to a User row (UserGroupType = "U") and the other points to a Group row (UserGroupType = "G")

     But I agree with David that I should stick with my current design and not complicate this.

    Are there any plans to allow tables to be marked (in case they have some extra columns or some uniqure structure) as many-to-many resolver (associative) tables so that mygenerations can create a class with Associate/Dissociate methods?

    Thanks

    Sarosh

View as RSS news feed in XML