David David - 4 months ago 18
MySQL Question

Phoenix Ecto ** (Mariaex.Error) (1054): Unknown column 'inserted_at' in 'field list'

When inserting a model into MySQL database using Elixir + Phoenix I get:

** (Mariaex.Error) (1054): Unknown column 'inserted_at' in 'field list'
stacktrace:
(ecto) lib/ecto/adapters/mysql.ex:181: Ecto.Adapters.MySQL.insert/5
(ecto) lib/ecto/repo/schema.ex:381: Ecto.Repo.Schema.apply/4
(ecto) lib/ecto/repo/schema.ex:185: anonymous fn/11 in Ecto.Repo.Schema.do_insert/4
(ecto) lib/ecto/repo/schema.ex:595: anonymous fn/3 in Ecto.Repo.Schema.wrap_in_transaction/6
(ecto) lib/ecto/adapters/sql.ex:472: anonymous fn/3 in Ecto.Adapters.SQL.do_transaction/3
(db_connection) lib/db_connection.ex:973: DBConnection.transaction_run/4
(db_connection) lib/db_connection.ex:897: DBConnection.run_begin/3
(db_connection) lib/db_connection.ex:671: DBConnection.transaction/3


This is not happening with other models, which work ok.
Model schema is:

schema "accounts" do
field :key, :string, null: false
field :cypher_key, :string, null: false
field :another_key, :string, null: false
field :another_cypher_key, :string, null: false
belongs_to :user, MyApp.User
timestamps()
end


and when inserting I'm doing:

Repo.insert! %Account{key: "test",
cypher_key: "test",
another_key: "test",
another_cypher_key: "pk_test"
}


When manually inserting via MySQL cmd it works ok.

Answer

Does your table contain timestamps fields? Did you generate proper migration for that?

Use mix ecto.gen.migration and then open file, which will be created in /priv/repo/migrations.

Alter the change function into something similar this:

def change do
  create table(:accounts) do
    add :key, :string
    # etc.

    timestamps
  end

  create unique_index(:accounts, [:key])
end

Then apply this migration with mix ecto.migrate.

I hope it will help.