Support many to many #183

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

Projects

None yet

8 participants

@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
Contributor
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

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
Contributor
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
Contributor
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
Contributor
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
Contributor
wkhere commented Apr 2, 2014

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

@josevalim josevalim added Up for grabs and removed Up for grabs labels Jan 14, 2015
@josevalim josevalim changed the title from many to many to Support many to many Jan 14, 2015
@josevalim
Member

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

@jontonsoup

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

@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

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
Member

@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

@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
Member

@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

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)

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

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