jonbirke0927

jonbirke0927

Configuring Phoenix generator to use a default schema when creating ecto schema

Hello everyone,

When using the phoenix generators to create a new context (i.e. mix phx.gen.context ContextName SchemaName table_name column_name:column_type), I continually run into an issue where the default schema being used in the new module is Ecto.Schema. The below code is an example of the code that would be generated using the command above:

defmodule MyApp.ContextName.SchemaName do
  use Ecto.Schema
  import Ecto.Changeset
  ...
end

My question is: is there a way to set the default schema to MyApp.Schema, instead of Ecto.Schema? If any app specific schema configurations have been implemented (such as primary_keys defaulting to uuid instead of integer_id), the generated schema will throw errors when you try to use it. It winds up being one of those problems that you don’t fix until you remember that you forgot to change use Ecto.Schema to use MyApp.Schema.

Thanks in advance!

First 2 of 2 Posts Switch mode

Matt

Matt

Hey Jonathan,

When you run a generator like phx.gen.context the content is obtained from a template. You can find the template in Phoenix’s source at /priv/templates/phx.gen.schema/schema.ex. When the generator is run, there is dynamic content generated such as the module name, fields, etc. But use Ecto.Schema is not a dynamically generated. Check it out:

# /priv/templates/phg.gen.schema/schema.ex

defmodule <%= inspect schema.module %> do
  use Ecto.Schema
  import Ecto.Changeset
<%= if schema.prefix do %>
  @schema_prefix :<%= schema.prefix %><% end %><%= if schema.binary_id do %>
  @primary_key {:id, :binary_id, autogenerate: true}
  @foreign_key_type :binary_id<% end %>
  schema <%= inspect schema.table %> do
<%= for {k, v} <- schema.types do %>    field <%= inspect k %>, <%= Mix.Phoenix.Schema.type_and_opts_for_schema(v) %><%= schema.defaults[k] %><%= Mix.Phoenix.Schema.maybe_redact_field(k in schema.redacts) %>
<% end %><%= for {_, k, _, _} <- schema.assocs do %>    field <%= inspect k %>, <%= if schema.binary_id do %>:binary_id<% else %>:id<% end %>
<% end %>
    timestamps()
  end

  @doc false
  def changeset(<%= schema.singular %>, attrs) do
    <%= schema.singular %>
    |> cast(attrs, [<%= Enum.map_join(schema.attrs, ", ", &inspect(elem(&1, 0))) %>])
    |> validate_required([<%= Enum.map_join(schema.attrs, ", ", &inspect(elem(&1, 0))) %>])
<%= for k <- schema.uniques do %>    |> unique_constraint(<%= inspect k %>)
<% end %>  end
end

You can check out the above source here.

So, to answer your question:

There is no configuration variable or command line argument that will achieve what you’re looking for. Some of your options may be:

  • Edit as you generate,
  • Automate the changes within your editor or a command line program like sed, or
  • Clone Phoenix and then edit the templates. You can then generate/move your project as you would if creating a project from an unreleased version. ← this seems messy to me.

If I was in your position, I would create an interactive Emacs command to take care of renaming Ecto.Schema to MyApp.Schema after generating a schema.

gregvaughn

gregvaughn

I vaguely remember that you can copy the Phoenix source file /priv/templates/phx.gen.schema/schema.ex to your own project and edit the file and this version will be used by the generators.

I couldn’t find docs, but I dug a bit deeper into the source and found that the helper module for the Phoenix generator tasks includes this function phoenix/lib/mix/phoenix.ex at v1.5.11 · phoenixframework/phoenix · GitHub

Which suggests you can copy the schema.ex, make your edit and future generators will use it. It’s worth a try.

— All posts loaded —

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

kraleppa
If you’ve ever had to debug a BEAM node you know it can be a painful process. Parsing raw data, or dealing with clutter often gets in the...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New

We're in Beta

About us Mission Statement