Skip to content

Use my own tables to generate join tables? #276

Closed

Description

@SamuelBolduc

I would like to know if it's possible to specify the join table name to use for a hasMany association? I would like to keep my SQL structure and test data from the application, but in some instances it's not possible in hasMany relations. My table names use underscores as well as my properties.

If for example I want to have a table role and a table app_module, linked by role_app_module, it doesn't work with hasMany. Is this possible to have a solution for this?

Thanks

Activity

  1. notheotherben commented on Aug 2, 2013

    @notheotherben
    Collaborator

    You should be able to set the merge_table property when defining your hasMany relationship.

    role.hasMany('app_modules', app_module, { ... }, {
        merge_table: 'role_app_module'
    });
  2. SamuelBolduc commented on Aug 2, 2013

    @SamuelBolduc
    Author

    Thanks for the reply? I did this :

    db.models.role.hasMany('app_module', db.models.app_module, {}, { 
      merge_table: 'role_app_module'
    });
    

    It compiles (but wouldn't without the third empty argument) but when I call role.getApp_modules, it says the method doesn't exist. I also tried getApp_modules. Maybe underscores break how getters and setters are generated?

  3. dresende commented on Aug 3, 2013

    @dresende
    Owner

    Documentation needs to be added for this, but for now here's more options you can use in the association:

    db.models.role.hasMany('app_module', db.models.app_module, {}, { 
      mergeTable: 'role_app_module',
      getAccessor: "getAppModules",
      setAccessor: "setAppModules",
      addAccessor: "setAppModules",
      delAccessor: "removeAppModules",
      hasAccessor: "hasAppModules"
    });

    If you don't set this options, it will create them based on the app_module.

    @spartan563 probably a good addition would be to have another key like accessor: "AppModules" that would be the template for get/set/...

  4. notheotherben commented on Aug 3, 2013

    @notheotherben
    Collaborator

    Sounds like a good idea to me, I'll see about getting it added and tested sometime today.

  5. notheotherben commented on Aug 3, 2013

    @notheotherben
    Collaborator

    I'll also add a reverseAccessor property so it's possible to set the template for reversed accessors.

  6. dresende commented on Aug 3, 2013

    @dresende
    Owner

    👍

  7. SamuelBolduc commented on Aug 6, 2013

    @SamuelBolduc
    Author

    Thank you very much for this! It was exactly what I needed. Also, SPARTAN563's addition worked very well for me!

    I think it would be a good addition to the docs!

  8. SamuelBolduc commented on Aug 13, 2013

    @SamuelBolduc
    Author

    Has this commit been merged to the master branch? I updated ORM twice recently (now at 2.1.0) and I had to manually apply this commit after both updates

  9. notheotherben commented on Aug 13, 2013

    @notheotherben
    Collaborator

    I don't believe the latest changes have made it into a release yet. We'll have to check with @dresende, but we'll likely only have a release when #288, #293 and a few others have been merged, tested and deemed stable.

  10. dresende commented on Aug 13, 2013

    @dresende
    Owner

    Yes, there are a few pending issues that we have to close. The next release will surely have this merged already. And will be released as soon as we close those issues.

  11. SamuelBolduc commented on Aug 13, 2013

    @SamuelBolduc
    Author

    Cool, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions