Phoenix Ecto **(Mariaex.Error)(1054):'字段列表'中的未知列'inserted_at'

时间:2016-08-09 11:32:00

标签: mysql elixir phoenix-framework

使用Elixir + Phoenix将模型插入MySQL数据库时,我得到:

** (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

其他型号没有发​​生这种情况,工作正常。 模型架构是:

  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

插入时我正在做:

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

当通过MySQL cmd手动插入时,它可以正常工作。

2 个答案:

答案 0 :(得分:0)

您的表格是否包含timestamps个字段?您是否为此生成了适当的迁移?

使用mix ecto.gen.migration然后打开文件,该文件将在/priv/repo/migrations中创建。

change函数更改为类似的内容:

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

    timestamps
  end

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

然后将此迁移应用于mix ecto.migrate

我希望它会有所帮助。

答案 1 :(得分:0)

您很可能没有在迁移文件中添加timestamps。对于Ecto.Schema和迁移文件,timestamps函数引用了两个字段:updated_atinserted_at。如果您碰巧来自Ruby on Rails,您可能会注意到created_at的命名差异。有关详细信息,请参阅the docs here

相关问题