簡體   English   中英

Elixir,phoenix,ecto:無法將數據添加到我的表中

[英]Elixir, phoenix, ecto : can't add data to my tables

我正在嘗試將行添加到我創建的表“用戶”中,但出現此錯誤:

iex(3)> user = %User{username: "mel", email: "mail"}
%Theme01.User{
  __meta__: #Ecto.Schema.Metadata<:built, "user">,
  id: nil,
  email: "mail",
  username: "mel",
  clocks: #Ecto.Association.NotLoaded<association :clocks is not loaded>,
  inserted_at: nil,
  updated_at: nil
}
iex(4)> user = Repo.insert(User)
** (FunctionClauseError) no function clause matching in Ecto.Repo.Schema.insert/4

    The following arguments were given to Ecto.Repo.Schema.insert/4:

        # 1
        Theme01.Repo

        # 2
        Theme01.Repo

        # 3
        Theme01.User

        # 4
        {%{
           adapter: Ecto.Adapters.Postgres,
           cache: #Reference<0.388300084.2616328193.258496>,
           opts: [
             repo: Theme01.Repo,
             timeout: 15000,
             pool_size: 10,
             pool: DBConnection.ConnectionPool
           ],
           pid: #PID<0.402.0>,
           repo: Theme01.Repo,
           sql: Ecto.Adapters.Postgres.Connection,
           stacktrace: true,
           telemetry: {Theme01.Repo, :debug, [:theme01, :repo, :query]}
         },
         [
           stacktrace: [
             {Ecto.Repo.Supervisor, :tuplet, 2,
              [file: 'lib/ecto/repo/supervisor.ex', line: 162]},
             {Theme01.Repo, :insert, 2, [file: 'lib/theme01/repo.ex', line: 2]},
             {:elixir, :"-eval_external_handler/1-fun-2-", 4,
              [file: 'src/elixir.erl', line: 298]},
             {:erl_eval, :do_apply, 7, [file: 'erl_eval.erl', line: 748]},
             {:erl_eval, :expr, 6, [file: 'erl_eval.erl', line: 492]},
             {:elixir, :eval_forms, 3, [file: 'src/elixir.erl', line: 288]},
             {Module.ParallelChecker, :verify, 1,
              [file: 'lib/module/parallel_checker.ex', line: 107]},
             {IEx.Evaluator, :eval_and_inspect, 3,
              [file: 'lib/iex/evaluator.ex', line: 329]},
             {IEx.Evaluator, :eval_and_inspect_parsed, 3,
              [file: 'lib/iex/evaluator.ex', line: 303]},
             {IEx.Evaluator, :parse_eval_inspect, 3,
              [file: 'lib/iex/evaluator.ex', line: 292]},
             {IEx.Evaluator, :loop, 1, [file: 'lib/iex/evaluator.ex', line: 187]},
             {IEx.Evaluator, :init, 4, [file: 'lib/iex/evaluator.ex', line: 32]},
             {:proc_lib, :init_p_do_apply, 3, [file: 'proc_lib.erl', line: 240]}
           ]
         ]}

    Attempted function clauses (showing 2 out of 2):

        def insert(repo, name, -%Ecto.Changeset{} = changeset-, tuplet)
        def insert(repo, name, -%{__struct__: _} = struct-, tuplet)

    (ecto 3.9.1) lib/ecto/repo/schema.ex:303: Ecto.Repo.Schema.insert/4
    iex:4: (file)

這是我的表的架構:

defmodule Theme01.User do
  use Ecto.Schema
  import Ecto.Changeset
  alias Theme01.Clock

  schema "user" do
    field :email, :string
    field :username, :string
    has_many(:clocks, Clock)

    timestamps()
  end

  @doc false
  def changeset(user, attrs) do
    user
    |> cast(attrs, [:username, :email])
    |> validate_required([:username, :email])
  end
end

這是遷移 function

defmodule Theme01.Repo.Migrations.CreateUser do
  use Ecto.Migration

  def change do
    create table("users") do
      add :username, :string, null: false
      add :email, :string, null: false

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

我已經堅持這個太久了,任何幫助將不勝感激

我檢查過我的表是否存在,當我在 psql 中運行 \dt 時它們確實出現了。 我還檢查了我的 config/dev.exs 是否設置為正確的數據庫。

Repo.insert(User)是嘗試將原子User (請參閱標題)插入表中。

相反,您將創建一個Ecto.Changeset ,然后將其插入數據庫,如下所示。

%User{}
|> User.changeset(attrs)
|> Repo.insert()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM