Skip to content

Commit

Permalink
Document how to use Ecto.ULID as prolific primary key
Browse files Browse the repository at this point in the history
  • Loading branch information
jc00ke authored and dcuddeback committed Apr 15, 2019
1 parent bde95db commit c2aa7ba
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,32 @@ create table(:events, primary_key: false) do
end
```

Alternatively, if you plan to use ULID as the primary key type for all of your tables, you can set
`migration_primary_key` when configuring your `Repo`:

```elixir
config :my_app, MyApp.Repo, migration_primary_key: [name: :id, type: :binary_id]
```

and then you *do not* need to specify the `id` column in your migrations:

```elixir
create table(:events) do
# more columns ...
end
```

### Schema

When defining a model's schema, use `Ecto.ULID` as the `@primary_key`:
When defining a model's schema, use `Ecto.ULID` as the `@primary_key` or `@foreign_key_type` as
appropriate for your schema. Here's an example of using both:

```elixir
defmodule MyApp.Event do
use Ecto.Schema

@primary_key {:id, Ecto.ULID, autogenerate: false}
@foreign_key_type Ecto.ULID
schema "events" do
# more columns ...
end
Expand Down

0 comments on commit c2aa7ba

Please sign in to comment.