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

using morphMany or other ways of representing a many to many relationship #22

Open
ononotofu opened this issue Jul 25, 2015 · 1 comment

Comments

@ononotofu
Copy link

hi there ! i am trying to adapt the endpoints example to work with something similar to Books having many Tags but Tags not being unique to a specific book (because there might also be Magazines and other Books that share the Tag). would this always require joining with a through table? what if I also want to have Magazines associated with the same Tags collection -- would that require a second table to join?

https://gist.github.com/ononotofu/cb268fd3c5d8b37da376 has some related implementation. is a polymorphic association appropriate without a "Books_Tags" table -- and i'd prefer to not have many similarly named joining tables created to create the relationship. it seems like there is probably a best practice for something that comes up this often, but i can't find the same relationship mocked up in an example in either bookshelf or endpoints. appreciate any help!

@tenzan
Copy link

tenzan commented Sep 29, 2015

Hi! I'm not sure if this will be an answer to your question, but I did the following way to establish many-to-many relations, using posts and tags example, where posts_tags is a join table.

in posts/model.js

const instanceProps = {
  tableName: 'posts',
  hasTimestamps: true,
  tags: function () {
    return this.belongsToMany(require('../tags/model'), 'posts_tags');
  }

in tags/model.js:

const instanceProps = {
  tableName: 'tags',
  hasTimestamps: true,
  posts: function () {
    return this.belongsToMany(require('../posts/model'), 'posts_tags');
  }
};

My repo https://github.com/tenzan/node-api-pg and deployed at http://node-api-pg.herokuapp.com/

This app shares a db with another rails app at http://postag.herokuapp.com/

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

No branches or pull requests

2 participants