Elixir 外星建筑协会don';不要把记录联系起来

Elixir 外星建筑协会don';不要把记录联系起来,elixir,Elixir,我有一个简单的用户/手机应用程序,每个用户都有许多手机 模型定义如下: user.ex defmodule UserManager.User do use Ecto.Schema schema "users" do field :email, :string has_many :phones, UserManager.Phone end end defmodule UserManager.Phone do use Ecto.Schema

我有一个简单的用户/手机应用程序,每个用户都有许多手机

模型定义如下:

user.ex

defmodule UserManager.User do
  use Ecto.Schema

  schema "users" do
    field :email, :string
    has_many :phones, UserManager.Phone
  end

end
defmodule UserManager.Phone do
  use Ecto.Schema

  schema "phones" do
    field :phone_number, :string
    field :phone_status, :string, default: "open"
    belongs_to :user, UserManager.User
  end

end
defmodule UserManager.Repo.Migrations.CreateUser do
  use Ecto.Migration

  def change do
    create table(:users) do
      add :email, :string
    end
  end
end
defmodule UserManager.Repo.Migrations.CreatePhone do
  use Ecto.Migration

  def change do
    create table(:phones) do
      add :phone_number, :string
      add :phone_status, :string
    end
  end
end
defmodule UserManager.Repo.Migrations.PhoneBelongsToUser do
  use Ecto.Migration

  def change do
    alter table(:phones) do
      add :user_id, references(:users)
    end

  end
end
phone.ex

defmodule UserManager.User do
  use Ecto.Schema

  schema "users" do
    field :email, :string
    has_many :phones, UserManager.Phone
  end

end
defmodule UserManager.Phone do
  use Ecto.Schema

  schema "phones" do
    field :phone_number, :string
    field :phone_status, :string, default: "open"
    belongs_to :user, UserManager.User
  end

end
defmodule UserManager.Repo.Migrations.CreateUser do
  use Ecto.Migration

  def change do
    create table(:users) do
      add :email, :string
    end
  end
end
defmodule UserManager.Repo.Migrations.CreatePhone do
  use Ecto.Migration

  def change do
    create table(:phones) do
      add :phone_number, :string
      add :phone_status, :string
    end
  end
end
defmodule UserManager.Repo.Migrations.PhoneBelongsToUser do
  use Ecto.Migration

  def change do
    alter table(:phones) do
      add :user_id, references(:users)
    end

  end
end
迁移

创建用户。ex

defmodule UserManager.User do
  use Ecto.Schema

  schema "users" do
    field :email, :string
    has_many :phones, UserManager.Phone
  end

end
defmodule UserManager.Phone do
  use Ecto.Schema

  schema "phones" do
    field :phone_number, :string
    field :phone_status, :string, default: "open"
    belongs_to :user, UserManager.User
  end

end
defmodule UserManager.Repo.Migrations.CreateUser do
  use Ecto.Migration

  def change do
    create table(:users) do
      add :email, :string
    end
  end
end
defmodule UserManager.Repo.Migrations.CreatePhone do
  use Ecto.Migration

  def change do
    create table(:phones) do
      add :phone_number, :string
      add :phone_status, :string
    end
  end
end
defmodule UserManager.Repo.Migrations.PhoneBelongsToUser do
  use Ecto.Migration

  def change do
    alter table(:phones) do
      add :user_id, references(:users)
    end

  end
end
创建_phone.ex

defmodule UserManager.User do
  use Ecto.Schema

  schema "users" do
    field :email, :string
    has_many :phones, UserManager.Phone
  end

end
defmodule UserManager.Phone do
  use Ecto.Schema

  schema "phones" do
    field :phone_number, :string
    field :phone_status, :string, default: "open"
    belongs_to :user, UserManager.User
  end

end
defmodule UserManager.Repo.Migrations.CreateUser do
  use Ecto.Migration

  def change do
    create table(:users) do
      add :email, :string
    end
  end
end
defmodule UserManager.Repo.Migrations.CreatePhone do
  use Ecto.Migration

  def change do
    create table(:phones) do
      add :phone_number, :string
      add :phone_status, :string
    end
  end
end
defmodule UserManager.Repo.Migrations.PhoneBelongsToUser do
  use Ecto.Migration

  def change do
    alter table(:phones) do
      add :user_id, references(:users)
    end

  end
end
手机属于用户。ex

defmodule UserManager.User do
  use Ecto.Schema

  schema "users" do
    field :email, :string
    has_many :phones, UserManager.Phone
  end

end
defmodule UserManager.Phone do
  use Ecto.Schema

  schema "phones" do
    field :phone_number, :string
    field :phone_status, :string, default: "open"
    belongs_to :user, UserManager.User
  end

end
defmodule UserManager.Repo.Migrations.CreateUser do
  use Ecto.Migration

  def change do
    create table(:users) do
      add :email, :string
    end
  end
end
defmodule UserManager.Repo.Migrations.CreatePhone do
  use Ecto.Migration

  def change do
    create table(:phones) do
      add :phone_number, :string
      add :phone_status, :string
    end
  end
end
defmodule UserManager.Repo.Migrations.PhoneBelongsToUser do
  use Ecto.Migration

  def change do
    alter table(:phones) do
      add :user_id, references(:users)
    end

  end
end
在iex上按如下方式运行代码:

alias UserManager.{Repo, User, Phone}
user = %User{email: "john@mail.com"}
Repo.insert!(user)
直到现在一切都运转良好。。。但是当我运行
exto.build\u assoc
elixir时,不要将手机与用户关联为什么?--ps:代码没有引发任何异常--


缺少的是在完成
Repo.insert之后使用
user=Repo.get(user,1)
获取用户!(用户)
。只有到那时,才能执行“外部构建”assoc

什么是“代码>行”?你什么时候打电话给
build\u assoc/3
?你检查过了吗?“行”是一个错误,它应该写“用户”。我已经正确地编辑了这个问题。我怀疑给DB打一个多余的电话是一个好方法<代码>%用户{电子邮件:john@mail.com“}|>build|assoc(:电话,电话号码:“052-8888”)|>Repo.insert!()应该足够好了。