-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Comments
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 |
you can setup many-to-many associations manually but there is no query support like you would get from Rails has_many :through like:
and then you get to query like obviously the syntax would be different but a has_many through option would be a good feature |
@patrickdet many-to-many associations are planned, but have not yet been implemented. I've marked this issue as "Up for grabs". |
Hi! 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? :) |
@herenowcoder Yes, there is. Pass Using the previous example: queryable "requirements", primary_key: false do
belongs_to :release, HexWeb.Release
belongs_to :dependency, HexWeb.Package
field :requirement, :string
end |
@ericmj I just happened to discover it by myself and was going to post here; thanks anyway! :D |
I wrote up how I did many-to-many joins here: http://www.coffeesforclosures.com/many-to-many-joins-with-ecto-and-elixir/ |
This depends on #357. We need to have a mechanism for updating associations on Repo.insert/update/delete before we start tackling this. |
Looking through the docs and noticed that this issue might be outdated? Re: http://hexdocs.pm/ecto/Ecto.Schema.html |
I looked and didn't see anything mentioning many-to-many. Was there something that jumped out at you? Sent from my phone
|
has_many :through is a many to many relationship.
|
@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. :) |
@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 the functionality described by @patrickdet is not the one aimed to be supported by this issue. We want to add |
Ah, I see — thanks for the clarification. Best, Jon On Mon, Jun 22, 2015 at 10:51 AM, José Valim notifications@github.com
|
Any solutions for many to many relationships? http://stackoverflow.com/questions/32900114/many-to-many-relationship-in-ecto |
I've opened a new issue to propose this feature as this already contains a long discussion. See #1169. |
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.
The text was updated successfully, but these errors were encountered: