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

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

This comment has been minimized.

Show comment
Hide comment
@ericmj

ericmj Feb 28, 2014

Member

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

Member

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

This comment has been minimized.

Show comment
Hide comment
@patrickdet

patrickdet 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

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 ericmj added Feature labels Mar 6, 2014

@ericmj

This comment has been minimized.

Show comment
Hide comment
@ericmj

ericmj Mar 6, 2014

Member

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

Member

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

This comment has been minimized.

Show comment
Hide comment
@wkhere

wkhere Apr 2, 2014

Contributor

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? :)

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

This comment has been minimized.

Show comment
Hide comment
@ericmj

ericmj Apr 2, 2014

Member

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

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

This comment has been minimized.

Show comment
Hide comment
@wkhere

wkhere Apr 2, 2014

Contributor

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

Contributor

wkhere commented Apr 2, 2014

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

@taybin

This comment has been minimized.

Show comment
Hide comment
@taybin

taybin commented Oct 3, 2014

@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

This comment has been minimized.

Show comment
Hide comment
@josevalim

josevalim Jan 29, 2015

Member

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

Member

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

This comment has been minimized.

Show comment
Hide comment
@jontonsoup

jontonsoup Jun 22, 2015

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

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

This comment has been minimized.

Show comment
Hide comment
@taybin

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

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

This comment has been minimized.

Show comment
Hide comment
@jontonsoup

jontonsoup 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:

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

This comment has been minimized.

Show comment
Hide comment
@josevalim

josevalim Jun 22, 2015

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. :)

Member

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

This comment has been minimized.

Show comment
Hide comment
@jontonsoup

jontonsoup 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?

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

This comment has been minimized.

Show comment
Hide comment
@josevalim

josevalim Jun 22, 2015

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.

Member

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

This comment has been minimized.

Show comment
Hide comment
@jontonsoup

jontonsoup 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)

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

This comment has been minimized.

Show comment
Hide comment
@terencechow

terencechow commented Oct 2, 2015

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

@josevalim

This comment has been minimized.

Show comment
Hide comment
@josevalim

josevalim Dec 25, 2015

Member

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

Member

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