Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use my own tables to generate join tables? #276

Closed
SamuelBolduc opened this issue Aug 2, 2013 · 11 comments
Closed

Use my own tables to generate join tables? #276

SamuelBolduc opened this issue Aug 2, 2013 · 11 comments

Comments

@SamuelBolduc
Copy link

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

@notheotherben
Copy link
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'
});

@SamuelBolduc
Copy link
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?

@dresende
Copy link
Owner

dresende commented Aug 3, 2013

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/...

@notheotherben
Copy link
Collaborator

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

@notheotherben
Copy link
Collaborator

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

notheotherben added a commit to notheotherben/node-orm2 that referenced this issue Aug 3, 2013
@dresende
Copy link
Owner

dresende commented Aug 3, 2013

👍

@SamuelBolduc
Copy link
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!

@SamuelBolduc
Copy link
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

@notheotherben
Copy link
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.

@dresende
Copy link
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.

@SamuelBolduc
Copy link
Author

Cool, thank you!

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

No branches or pull requests

3 participants