ash icon indicating copy to clipboard operation
ash copied to clipboard

Improve many_to_many default options

Open totaltrash opened this issue 4 years ago • 4 comments

Is your feature request related to a problem? Please describe. Many to many relationships are a little verbose. Also, the current default for destination_field_on_join_table would use the relationship name (tags_id), instead of the resource name (tag_id)

Describe the solution you'd like The source and destination join table fields in many to many relationships can be derived with the following defaults:

source_field_on_join_table: this_resource_id destination_field_on_join_table: destination_resource_id

Describe alternatives you've considered Stick with the current (but fix the default for destination_field_on_join_table)

Express the feature either with a change to resource syntax, or with a change to the resource interface

This would result in a nice declaration for many_to_many relationships:

defmodule MyApp.Blog.Post do
  relationships do
    many_to_many :tags, MyApp.Blog.Tag do
      through MyApp.Blog.PostTag
    end
  end
end

totaltrash avatar Jun 02 '21 02:06 totaltrash

Looking at the code, I actually don't see any place that sets the default. So there is, in fact, no default value for destination_field_on_join_table and source_field_on_join_table. If anyone wants to tackle this, it should be a relatively straightforward addition of a transformer module similar to this one: https://github.com/ash-project/ash/blob/master/lib/ash/resource/transformers/has_destination_field.ex

zachdaniel avatar Jun 02 '21 15:06 zachdaniel

Cool, I'll have a look at this

totaltrash avatar Jun 03 '21 01:06 totaltrash

We set these defaults now.

zachdaniel avatar Sep 10 '22 02:09 zachdaniel

Actually...we don't :)

zachdaniel avatar Sep 10 '22 02:09 zachdaniel