2016-08-09 56 views
1

當使用藥劑+鳳凰插入模型到MySQL數據庫我得到:鳳凰外生**(Mariaex.Error)(1054):未知列 'inserted_at' 在 '字段列表'

** (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它工作正常。

回答

0

您的表格是否包含timestamps字段?你是否爲此產生了適當的遷移?

使用mix ecto.gen.migration然後打開文件,該文件將在中創建。

改變change功能到類似這樣的:

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

    timestamps 
    end 

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

然後用mix ecto.migrate應用此遷移。

我希望這會有所幫助。

+0

我錯過了,謝謝指出。 – David

0

很可能您沒有在遷移文件中添加timestamps。對於Ecto.Schema和遷移文件,timestamps函數引用兩個字段:updated_atinserted_at。如果您恰巧來自Ruby on Rails,您可能會注意到與created_at的命名不同。有關更多詳細信息,請參見the docs here

+0

我錯過了,謝謝指出。 – David

相關問題