Hey everyone,
I’m wanting to apply a query fragment within the Phoenix application’s config.exs in order to properly pass a default value to the Ecto.Migration
runner.
my_app/config/config.exs
is setup like so:
config :my_app, MyApp.Repo,
migration_primary_key: [name: :id, type: :uuid, default: "gen_random_uuid()"],
migration_foreign_key: [column: :id, type: :uuid]
I’m trying to have the default :primary_key of every new table use UUID and have a default value that generates a random uuid.
And migrating produces this SQL statement in Ecto.Adapter
, but it results in an error from the database. (using Postgres)
CREATE TABLE "users" ("id" uuid DEFAULT 'gen_random_uuid()', "name" varchar(255) NOT NULL, "type" integer NOT NULL, "inserted_at" timestamp(0) NOT NULL, "updated_at" timestamp(0) NOT NULL, PRIMARY KEY ("id"))
** (Postgrex.Error) ERROR 22P02 (invalid_text_representation) Invalid UUID: incorrect size
The migration is so very close, but sadly invalid because the default value is being passed as a fixed string but needs to be a Ecto.Query.API.fragment/1
.
And sadly due to the config not having any dependencies available to pull from, importing the necessary Module to do this.
error: module Ecto.Query.API is not loaded and could not be found
│
│ import Ecto.Query.API
│ ^^^^^^^^^^^^^^^^^^^^^
│
└─ config/config.exs:10
It would be really awesome to have these migrations automatically have a working default value for primary ids without needing to manually apply them in every new migration.
If there is no way to do this in the Config, does anyone have a galaxy-brained solution for the outcome I’m looking for?
Thanks