purplerice

purplerice

Mix test gives error of column "inserted_at"

Hi,

Newbie to Elixir/Phoenix. I come from a Rails background but I’m loving Elixir/Phoenix so far.

When I try to run “mix test”, I’m getting the following error:

** (Postgrex.Error) ERROR (undefined_column): column "inserted_at" of relation "schema_migrations" does not exist
(ecto) lib/ecto/adapters/sql.ex:463: Ecto.Adapters.SQL.struct/6
(ecto) lib/ecto/repo/schema.ex:397: Ecto.Repo.Schema.apply/4
(ecto) lib/ecto/repo/schema.ex:193: anonymous fn/11 in Ecto.Repo.Schema.do_insert/4
(ecto) lib/ecto/repo/schema.ex:124: Ecto.Repo.Schema.insert!/4
(ecto) lib/ecto/adapters/sql.ex:508: anonymous fn/3 in Ecto.Adapters.SQL.do_transaction/3
(db_connection) lib/db_connection.ex:1274: DBConnection.transaction_run/4
(db_connection) lib/db_connection.ex:1198: DBConnection.run_begin/3
(db_connection) lib/db_connection.ex:789: DBConnection.transaction/3

My migrations is as follows:
def change do
drop_if_exists table(:users)

    create table(:users, primary_key: false) do
      add :username, :string, primary_key: true, null: false
      add :password, :string, null: false
      add :nickname, :string, unique: true
      add :verification_code, :string
      add :phone_verified, :boolean, default: false, null: false
      add :max_distance, :integer
      add :distance_unit, :string, default: "km", null: false

      timestamps([{:inserted_at,:created_at}, {:updated_at, false}])
    end

    create unique_index(:users, [:username])
    create unique_index(:users, [:nickname])
  end

My schema is as follows:

  @primary_key {:username, :string, []}
  @derive {Phoenix.Param, key: :username}

  schema "users" do
    field :nickname, :string
    field :verification_code, :string
    field :phone_verified, :boolean, default: false
    field :password, :string
    field :max_distance, :integer
    field :distance_unit, :string

     timestamps([{:inserted_at,:created_at}, {:updated_at, false}])
  end

  @doc """
  Builds a changeset based on the `struct` and `params`.
  """
  def changeset(struct, params \\ %{}) do
    struct
    |> cast(params, [:nickname, :verification_code, :phone_verified, :password, :max_distance, :distance_unit])
    |> validate_required([:username, :nickname, :phone_verified, :password])
  end

When I look in my database, I see that the “schema_migrations” table does indeed have an “inserted_at” column, which is where I’m assuming the error is coming from but I haven’t created that column (as far as I can tell).

Any insight?

Most Liked

purplerice

purplerice

Thank you all for your help. First time I’ve interacted with the Elixir community and it’s been pleasantly supportive and helpful. With your responses in mind, I started digging deeper into the test database to see whether it could be a permission issue. Turns out, my test database was actually the one created with Ruby on Rails (it had the same name). Once I deleted the RoR created database and reran tests it worked fine. Thanks again for all the help.

jwarlander

jwarlander

Nothing helps your learning better than having to dig in and troubleshoot a bit, in my experience :grin:

mgwidmann

mgwidmann

I’m with @jwarlander in thinking that the test database and schema_migrations table were created by another database user and so therefore the user you’re running with doesn’t have access. Try running mix test but changing the database config in config/test.exs to use the different users you have set up…

As @benwilson512 pointed out, you want to make sure your mix.exs has in the aliases/0 function something like

"test": ["ecto.create --quiet", "ecto.migrate", "test"]

which will make sure you’re creating the database and migrating it before running your tests.

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
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
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement