gjaldon / ecto_enum

Ecto extension to support enums in models
MIT License
562 stars 131 forks source link

default value with enum #95

Closed zhisme closed 5 years ago

zhisme commented 5 years ago

Hey there. I searched all your repo and source code and couldn't find whether I can set default value for enum during migration.

Expected Result

def change do
  StatusEnum.create_type()

  create table(:transactions) do
     add :status, StatusEnum.type(), default: 0 # or :init

     timestamps()
  end
end

=> OK

Actual Result

** (Postgrex.Error) ERROR 42804 (datatype_mismatch) column "status" is of type status but default expression is of type integer

    hint: You will need to rewrite or cast the expression.
    (ecto_sql) lib/ecto/adapters/sql.ex:621: Ecto.Adapters.SQL.raise_sql_call_error/1
    (elixir) lib/enum.ex:1294: Enum."-map/2-lists^map/1-0-"/2
    (ecto_sql) lib/ecto/adapters/sql.ex:708: Ecto.Adapters.SQL.execute_ddl/4
    (ecto_sql) lib/ecto/migration/runner.ex:340: Ecto.Migration.Runner.log_and_execute_ddl/3
    (ecto_sql) lib/ecto/migration/runner.ex:119: anonymous fn/6 in Ecto.Migration.Runner.flush/0
cflegel commented 5 years ago

This works for me:

def change do
  StatusEnum.create_type()

  create table(:transactions) do
     add :status, StatusEnum.type(), default: "init"

     timestamps()
  end
end
zhisme commented 5 years ago

@cflegel yes, this one works. Thanks!