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

Support many to many #183

Closed
jschoch opened this issue Feb 28, 2014 · 17 comments
Closed

Support many to many #183

jschoch opened this issue Feb 28, 2014 · 17 comments
Milestone

Comments

@jschoch
Copy link

@jschoch jschoch commented Feb 28, 2014

does ecto support many to many? If so maybe a section on the main page on how this works, the tests for associations doesn't seem to test this case.

@ericmj
Copy link
Member

@ericmj ericmj commented Feb 28, 2014

Currently you have to setup many-to-many associations yourself with an extra model setting up the associations. Here is an example: https://github.com/ericmj/hex_web/blob/master/lib/hex_web/requirement.ex

@patrickdet
Copy link

@patrickdet patrickdet commented Mar 6, 2014

you can setup many-to-many associations manually but there is no query support like you would get from Rails has_many :through like:

class Post
  has_many :comments
  has_many :commenters, through: :comments

class Comment
  belongs_to :post 
  belongs_to :commenter, class_name: "User"

class User
  has_many :comments

and then you get to query like @post.commenters

obviously the syntax would be different but a has_many through option would be a good feature

@ericmj
Copy link
Member

@ericmj ericmj commented Mar 6, 2014

@patrickdet many-to-many associations are planned, but have not yet been implemented. I've marked this issue as "Up for grabs".

@wkhere
Copy link
Contributor

@wkhere wkhere commented Apr 2, 2014

Hi!
Thx @ericmj for the example with an extra model between many-to-many peers.

However, there's one catch: I just checked that such model needs that intermediate table has id column with an artificial primary key. While this can be acceptable when using Rails-created databases (where artificial id on new intermediate tables is a default), I work every day with lots of legacy tables created for solving many-to-many relationships and they have only a composite key consisting of two referenced ids. Usually these two ids are the only columns in such table. Unfortunately I don't have means of adding artificial id in most of such cases, thus it's rather blocking adoption of Ecto for db schemas like these..

Is there a way to simply tell to Ecto that id column is not necessary in some cases? I guess it could complicate query macros a bit.. or not? :)

@ericmj
Copy link
Member

@ericmj ericmj commented Apr 2, 2014

@herenowcoder Yes, there is. Pass primary_key: false to queryable.

Using the previous example:

queryable "requirements", primary_key: false do
  belongs_to :release, HexWeb.Release
  belongs_to :dependency, HexWeb.Package
  field :requirement, :string
end
@wkhere
Copy link
Contributor

@wkhere wkhere commented Apr 2, 2014

@ericmj I just happened to discover it by myself and was going to post here; thanks anyway! :D

@taybin
Copy link

@taybin taybin commented Oct 3, 2014

@josevalim josevalim changed the title many to many Support many to many Jan 14, 2015
@josevalim
Copy link
Member

@josevalim josevalim commented Jan 29, 2015

This depends on #357. We need to have a mechanism for updating associations on Repo.insert/update/delete before we start tackling this.

@jontonsoup
Copy link

@jontonsoup jontonsoup commented Jun 22, 2015

Looking through the docs and noticed that this issue might be outdated? Re: http://hexdocs.pm/ecto/Ecto.Schema.html

@taybin
Copy link

@taybin taybin commented Jun 22, 2015

I looked and didn't see anything mentioning many-to-many. Was there something that jumped out at you?

Sent from my phone

On Jun 21, 2015, at 9:40 PM, Jonathan Friedman notifications@github.com wrote:

Looking through the docs and noticed that this issue might be outdated? Re: http://hexdocs.pm/ecto/Ecto.Schema.html


Reply to this email directly or view it on GitHub.

@jontonsoup
Copy link

@jontonsoup jontonsoup commented Jun 22, 2015

has_many :through is a many to many relationship.

In the example above, we have defined a has_many :through association named :comments_authors. A :through association always expect a list and the first element of the list must be a previously defined association in the current module. For example, :comments_authors first points to :comments in the same module (Post), which then points to :author in the next model Comment.

This :through associations will return all authors for all comments that belongs to that post:
@josevalim
Copy link
Member

@josevalim josevalim commented Jun 22, 2015

@jontonsoup indeed.

We also want to support more direct many to many which automatically manages the intermediate table. And that's what this issue is about. :)

@jontonsoup
Copy link

@jontonsoup jontonsoup commented Jun 22, 2015

@josevalim I'm slightly confused. The essential functionality that @patrickdet 's comment describes on Mar 6, 2014 seems to have been implemented according to the docs, although not fully. I must be missing something?

@josevalim
Copy link
Member

@josevalim josevalim commented Jun 22, 2015

@jontonsoup the functionality described by @patrickdet is not the one aimed to be supported by this issue. We want to add many_to_many "tags", Tag or something similar.

@jontonsoup
Copy link

@jontonsoup jontonsoup commented Jun 22, 2015

Ah, I see — thanks for the clarification. 

Best,

Jon

On Mon, Jun 22, 2015 at 10:51 AM, José Valim notifications@github.com
wrote:

@jontonsoup the functionality described by @patrickdet is not the one aimed to be supported by this issue. We want to add many_to_many "tags", Tag or something similar.

Reply to this email directly or view it on GitHub:
#183 (comment)

@terencechow
Copy link

@terencechow terencechow commented Oct 2, 2015

@josevalim josevalim added this to the v2.0 milestone Dec 15, 2015
@josevalim
Copy link
Member

@josevalim josevalim commented Dec 25, 2015

I've opened a new issue to propose this feature as this already contains a long discussion. See #1169.

@josevalim josevalim closed this Dec 25, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
8 participants