Composite primary keys in Phoenix routes and helpers

It seems like Phoenix does not support composite primary keys in resources macro, path helpers and router.

  • resources macro has :param option which accepts no list
  • path helpers need to extract fields which form primary key from provided struct and return appropriate URL like this:
    user_path(MyApp.Endpoint, :show, %User{first_name: "foo", last_name: "bar") "/api/users/foo,bar"
  • resources macro should generate a route like this /api/users/:first_name,:last_name and the router should dispatch both fields to controller show action in params map.

Resource is just a helper, it is pretty easy to compose your own paths instead and just call get/put/etc… straight. I never use the resource macro because nothing I make fits properly into it. ^.^;

1 Like

What you’re looking for is https://hexdocs.pm/phoenix/1.3.0/Phoenix.Param.html.

It lets you define how you extract an id for a route for a given struct.

1 Like