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

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
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
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement