简体   繁体   English

Elixir,phoenix,ecto:无法将数据添加到我的表中

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

I am trying to add rows to the table 'users' I have created, but I get this error:我正在尝试将行添加到我创建的表“用户”中,但出现此错误:

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)

Here is the schema of my table:这是我的表的架构:

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

and here is the migration function这是迁移 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

I've been stuck on this for way too long, any help will be greatly appreciated我已经坚持这个太久了,任何帮助将不胜感激

I've checked that my tables exist, and they do appear when I run \dt in psql.我检查过我的表是否存在,当我在 psql 中运行 \dt 时它们确实出现了。 I've also checked that my config/dev.exs is set for the correct database.我还检查了我的 config/dev.exs 是否设置为正确的数据库。

Repo.insert(User) is an attempt to insert an atom User (see titlecase) into the table. Repo.insert(User)是尝试将原子User (请参阅标题)插入表中。

Instead, you are to create an Ecto.Changeset and then insert it into the database, somewhat like below.相反,您将创建一个Ecto.Changeset ,然后将其插入数据库,如下所示。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM