Phx.gen.live with my custom field as 'id'

When generating live views through phx.gen.live the entire set of table, schema and corresponding liveviews (.ex and heex), router links are generated with a default id field that is not even visually present in the schema definition.

I wonder if there a setting of a flag that lets me specify the name of that ‘id’ name with me custom name, like. ‘country_id’, ‘product_id’ or similar so I don’t need to manually search and rename all the instances of it to change it. Thanks

There will be in the upcoming Phoenix 1.8 generators: Add ability to customize the primary key name by saleyn · Pull Request #5766 · phoenixframework/phoenix · GitHub

oooohhh… this is awesome. Thanks!

you mean Phoenix 1.7.18? The one just released?

No, sorry, it’s only going to be in Phoenix 1.8 which is still work in progress.

Got it, thanks

If you want to have them now, or want to use your own templates, you can create a file for each template in priv/templates/phx.gen.live/ with your custom template code.

Phoenix will try that path first when rendering the templates. phoenix/lib/mix/phoenix.ex at e0fa893af6122bd89eadd379ae9b450bee34cc7b · phoenixframework/phoenix · GitHub

Thanks. This is very new to me.
How would it work, I would change the default “id” for what I need only inside the “live” templates, but not inside the “context”?
And if I need to create various different views, then I’d need to create a template by template and change the field before each new generation?
Is that it?

Sorry, after re-reading your question, I realize that my answer does not solve what you are asking. I thought you only wanted to change the routes in the Heex templates.

Maybe you can achieve this with the Phoenix.Param protocol? Phoenix.Param — Phoenix v1.7.18

To be honest, maybe I do want to change it only in HEEX as my final objective is to use it in url params. I thought to do it properly I’d need to change it everywhere, but I think your option of using Phoenix.Params might indeed be enough for my use case. Thanks.

1 Like