View Source Using Ksuid as the Default
If you are starting a new project with ecto, it may be desirable to use ksuids as the default in your system. This guide will guide you through the steps needed.
configuring-migrations
Configuring Migrations
This is very similar to configuring your migrations to use a :binary_id
as a
default. See also the Repo Configuration section of Ecto.Migration
.
Configure your Repo.
# config/config.exs config :my_app, MyApp.Repo, migration_primary_key: [type: :"char(27)"], migration_foreign_key: [type: :"char(27)"]
Create migrations as you normally would.
# priv/repo/migrations/create_posts.exs defmodule MyApp.Repo.Migrations.CreatePosts do use Ecto.Migration def change() do create table(:posts) do add :title, :string end create table(:comments) do add :post_id, references(:posts) end end end
Now, any migrations that will add a :primary_key
of :id
with the EctoKsuid
type. Additionally, any references will use the EctoKsuid
type.
configuring-schemas
Configuring Schemas
This is very similar to configuring your application to use a :binary_id
as
default. See also the Schema Attributes section of Ecto.Schema
.
Define a module to be use as a base
# lib/my_app/schema.ex defmodule MyApp.Schema do defmacro __using__(_) do quote do use Ecto.Schema @primary_key {:id, EctoKsuid, autogenerate: true} @foreign_key_type EctoKsuid end end end
Now use
MyApp.Schema
instead ofEcto.Schema
to define new schemas in your application.# lib/my_app/comment.ex defmodule MyApp.Comment do use MyApp.Schema schema "comments" do belongs_to :post, MyApp.Post end end
Now any schemas using MyApp.Schema
will get the :id
field with the type of
EctoKsuid
as the primary key.
The belongs_to
association on MyApp.Comment
will also define a :post_id
field with the EctoKsuid
type that references the :id
field of the
MyApp.Post