chensan

chensan

(Postgrex.Error) ERROR 42804 (datatype_mismatch): column "" cannot be cast automatically to type integer

I have a User schema with a :from_id field set to type :string:

defmodule TweetBot.Repo.Migrations.CreateUsers do
  use Ecto.Migration

  def change do
    create table(:users) do
      add :from_id, :string
      add :access_token, :string

      timestamps()
    end

    create unique_index(:users, [:from_id])
  end
end

Later I want to change type of :from_id to :integer,

defmodule TweetBot.Repo.Migrations.AlterUsers do
  use Ecto.Migration

  def change do
    alter table(:users) do
      modify(:from_id, :integer)
    end
  end
end

But I got this error running mix ecto.migrate:

** (Postgrex.Error) ERROR 42804 (datatype_mismatch): column “from_id” cannot be cast automatically to type integer

I just thought it might because I have data in database, so I drop the whole database, and run mix ecto.migrate again, the error still shows.

I also search the ecto repo on github, no simillar issue there. Seems there’s an answer on stackoverflow https://stackoverflow.com/questions/13170570/change-type-of-varchar-field-to-integer-cannot-be-cast-automatically-to-type-i for Rails, but how can I do it in ecto?

Thanks if anyone can help.

Marked As Solved

fmcgeough

fmcgeough

Yeah. That’s a Postgresql restriction. You’ll have to execute SQL to convert it.

alter table users alter column from_id type integer using (from_id::integer);

if you use psql and attempt to do the SQL directly it actually provides a hint on this:

HINT: You might need to specify “USING from_id::integer”.

so:

defmodule TweetBot.Repo.Migrations.AlterUsers do
  use Ecto.Migration

   def up do
      execute """
        alter table users alter column from_id type integer using (from_id::integer)
       """
   end

   def down do
      execute """
        alter table users alter column from_id type character varying(255);
       """
   end
end
16
Post #2

Also Liked

OvermindDL1

OvermindDL1

@fmcgeough execute/2 exists that takes both an up and down argument so you don’t need to split them. :slight_smile:

fmcgeough

fmcgeough

Oooo thanks! I wasn’t aware of that!

chensan

chensan

@OvermindDL1 @fmcgeough Thank you both for your help :slight_smile:

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement