Created
January 28, 2019 16:00
-
-
Save angelikatyborska/a68ab88a3dd838d2cfe2feca1ffd545f to your computer and use it in GitHub Desktop.
UUIDs with Ecto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyApp.Repo.Migrations.AddUuidOssp do | |
use Ecto.Migration | |
def up do | |
execute("CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\" WITH SCHEMA public;") | |
end | |
def down do | |
execute("DROP EXTENSION \"uuid-ossp\";") | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyApp.Repo.Migrations.AddModel do | |
use Ecto.Migration | |
def change do | |
create table(:model, primary_key: false) do | |
add(:id, :uuid, primary_key: true, default: fragment("uuid_generate_v4()")) | |
timestamps() | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyApp.Model do | |
use MyApp, :model | |
schema "model" do | |
timestamps() | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyApp do | |
def model do | |
quote do | |
use Ecto.Schema | |
import Ecto | |
import Ecto.Changeset | |
@primary_key {:id, :binary_id, autogenerate: false, read_after_writes: true} | |
@foreign_key_type :binary_id | |
end | |
end | |
defmacro __using__(which) when is_atom(which) do | |
apply(__MODULE__, which, []) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment