Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do you convert an existing string column to the enum type ? #55

Open
happysalada opened this issue Feb 7, 2018 · 1 comment
Open

Comments

@happysalada
Copy link

just modifying the column to the created enum type gives an error

** (Postgrex.Error) ERROR 42804 (datatype_mismatch): column "origin" cannot be cast automatically to type origin
    (ecto) lib/ecto/adapters/sql.ex:200: Ecto.Adapters.SQL.query!/5
    (ecto) lib/ecto/adapters/postgres.ex:85: anonymous fn/4 in Ecto.Adapters.Postgres.execute_ddl/3
    (elixir) lib/enum.ex:1899: Enum."-reduce/3-lists^foldl/2-0-"/3
    (ecto) lib/ecto/adapters/postgres.ex:85: Ecto.Adapters.Postgres.execute_ddl/3
    (ecto) lib/ecto/migration/runner.ex:104: anonymous fn/2 in Ecto.Migration.Runner.flush/0
    (elixir) lib/enum.ex:1899: Enum."-reduce/3-lists^foldl/2-0-"/3
    (ecto) lib/ecto/migration/runner.ex:102: Ecto.Migration.Runner.flush/0
    (stdlib) timer.erl:181: :timer.tc/2
    (ecto) lib/ecto/migration/runner.ex:26: Ecto.Migration.Runner.run/6
    (ecto) lib/ecto/migrator.ex:128: Ecto.Migrator.attempt/6
    (ecto) lib/ecto/migrator.ex:72: anonymous fn/4 in Ecto.Migrator.do_up/4
    (ecto) lib/ecto/adapters/sql.ex:576: anonymous fn/3 in Ecto.Adapters.SQL.do_transaction/3
    (db_connection) lib/db_connection.ex:1283: DBConnection.transaction_run/4
    (db_connection) lib/db_connection.ex:1207: DBConnection.run_begin/3
    (db_connection) lib/db_connection.ex:798: DBConnection.transaction/3
    (ecto) lib/ecto/migrator.ex:261: anonymous fn/4 in Ecto.Migrator.migrate/4
    (elixir) lib/enum.ex:1294: Enum."-map/2-lists^map/1-0-"/2
    (ecto) lib/mix/tasks/ecto.migrate.ex:83: anonymous fn/4 in Mix.Tasks.Ecto.Migrate.run/2
    (elixir) lib/enum.ex:737: Enum."-each/2-lists^foreach/1-0-"/2
    (elixir) lib/enum.ex:737: Enum.each/2
[error] GenServer Union.Repo terminating
** (Postgrex.Error) ERROR 42804 (datatype_mismatch): column "origin" cannot be cast automatically to type origin
    (ecto) lib/ecto/adapters/sql.ex:200: Ecto.Adapters.SQL.query!/5
    (ecto) lib/ecto/adapters/postgres.ex:85: anonymous fn/4 in Ecto.Adapters.Postgres.execute_ddl/3
    (elixir) lib/enum.ex:1899: Enum."-reduce/3-lists^foldl/2-0-"/3
    (ecto) lib/ecto/adapters/postgres.ex:85: Ecto.Adapters.Postgres.execute_ddl/3
    (ecto) lib/ecto/migration/runner.ex:104: anonymous fn/2 in Ecto.Migration.Runner.flush/0
    (elixir) lib/enum.ex:1899: Enum."-reduce/3-lists^foldl/2-0-"/3
    (ecto) lib/ecto/migration/runner.ex:102: Ecto.Migration.Runner.flush/0
    (stdlib) timer.erl:181: :timer.tc/2
    (ecto) lib/ecto/migration/runner.ex:26: Ecto.Migration.Runner.run/6
    (ecto) lib/ecto/migrator.ex:128: Ecto.Migrator.attempt/6
    (ecto) lib/ecto/migrator.ex:72: anonymous fn/4 in Ecto.Migrator.do_up/4
    (ecto) lib/ecto/adapters/sql.ex:576: anonymous fn/3 in Ecto.Adapters.SQL.do_transaction/3
    (db_connection) lib/db_connection.ex:1283: DBConnection.transaction_run/4
    (db_connection) lib/db_connection.ex:1207: DBConnection.run_begin/3
    (db_connection) lib/db_connection.ex:798: DBConnection.transaction/3
    (ecto) lib/ecto/migrator.ex:261: anonymous fn/4 in Ecto.Migrator.migrate/4
    (elixir) lib/enum.ex:1294: Enum."-map/2-lists^map/1-0-"/2
    (ecto) lib/mix/tasks/ecto.migrate.ex:83: anonymous fn/4 in Mix.Tasks.Ecto.Migrate.run/2
    (elixir) lib/enum.ex:737: Enum."-each/2-lists^foreach/1-0-"/2
    (elixir) lib/enum.ex:737: Enum.each/2
Last message: {:EXIT, #PID<0.73.0>, {%Postgrex.Error{connection_id: 42580, message: nil, postgres: %{code: :datatype_mismatch, file: "tablecmds.c", hint: "You might need to specify \"USING origin::origin\".", line: "8007", message: "column \"origin\" cannot be cast automatically to type origin", pg_code: "42804", routine: "ATPrepAlterColumnType", severity: "ERROR", unknown: "ERROR"}}, [{Ecto.Adapters.SQL, :query!, 5, [file: 'lib/ecto/adapters/sql.ex', line: 200]}, {Ecto.Adapters.Postgres, :"-execute_ddl/3-fun-0-", 4, [file: 'lib/ecto/adapters/postgres.ex', line: 85]}, {Enum, :"-reduce/3-lists^foldl/2-0-", 3, [file: 'lib/enum.ex', line: 1899]}, {Ecto.Adapters.Postgres, :execute_ddl, 3, [file: 'lib/ecto/adapters/postgres.ex', line: 85]}, {Ecto.Migration.Runner, :"-flush/0-fun-1-", 2, [file: 'lib/ecto/migration/runner.ex', line: 104]}, {Enum, :"-reduce/3-lists^foldl/2-0-", 3, [file: 'lib/enum.ex', line: 1899]}, {Ecto.Migration.Runner, :flush, 0, [file: 'lib/ecto/migration/runner.ex', line: 102]}, {:timer, :tc, 2, [file: 'timer.erl', line: 181]}, {Ecto.Migration.Runner, :run, 6, [file: 'lib/ecto/migration/runner.ex', line: 26]}, {Ecto.Migrator, :attempt, 6, [file: 'lib/ecto/migrator.ex', line: 128]}, {Ecto.Migrator, :"-do_up/4-fun-1-", 4, [file: 'lib/ecto/migrator.ex', line: 72]}, {Ecto.Adapters.SQL, :"-do_transaction/3-fun-1-", 3, [file: 'lib/ecto/adapters/sql.ex', line: 576]}, {DBConnection, :transaction_run, 4, [file: 'lib/db_connection.ex', line: 1283]}, {DBConnection, :run_begin, 3, [file: 'lib/db_connection.ex', line: 1207]}, {DBConnection, :transaction, 3, [file: 'lib/db_connection.ex', line: 798]}, {Ecto.Migrator, :"-migrate/4-fun-0-", 4, [file: 'lib/ecto/migrator.ex', line: 261]}, {Enum, :"-map/2-lists^map/1-0-", 2, [file: 'lib/enum.ex', line: 1294]}, {Mix.Tasks.Ecto.Migrate, :"-run/2-fun-1-", 4, [file: 'lib/mix/tasks/ecto.migrate.ex', line: 83]}, {Enum, :"-each/2-lists^foreach/1-0-", 2, [file: 'lib/enum.ex', line: 737]}, {Enum, :each, 2, [file: 'lib/enum.ex', line: 737]}]}}
State: {:state, {:local, Union.Repo}, :one_for_one, [{:child, #PID<0.259.0>, DBConnection.Poolboy, {:poolboy, :start_link, [[name: {:local, Union.Repo.Pool}, strategy: :fifo, size: 1, max_overflow: 0, worker_module: DBConnection.Poolboy.Worker], {Postgrex.Protocol, [types: Ecto.Adapters.Postgres.TypeModule, port: 5432, name: Union.Repo.Pool, otp_app: :union, repo: Union.Repo, timeout: 15000, pool_timeout: 5000, adapter: Ecto.Adapters.Postgres, username: "postgres", password: "92009gtk9", database: "mylocaldb", hostname: "localhost", pool_size: 1, pool: DBConnection.Poolboy]}]}, :permanent, 5000, :worker, [:poolboy]}], :undefined, 3, 5, [], 0, Ecto.Repo.Supervisor, {Union.Repo, :union, Ecto.Adapters.Postgres, [pool_size: 1]}}
@gabrielpra1
Copy link

gabrielpra1 commented Jan 13, 2019

This is how I did to change the column role from my users table from string to UserRoleEnum.

def up do
  UserRoleEnum.create_type
  execute "alter table users alter column role type role using (role::role)"
end

def down do
  execute "alter table users alter column role type character varying(255)"
  UserRoleEnum.drop_type
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants