简体   繁体   中英

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

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. I've also checked that my config/dev.exs is set for the correct database.

Repo.insert(User) is an attempt to insert an atom User (see titlecase) into the table.

Instead, you are to create an Ecto.Changeset and then insert it into the database, somewhat like below.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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