2
나는 두 models, Person
및 Pet
를 가지고 있고, 나는 Person
가 have many 애완 동물에 할 수 있도록하려면,하지만 Pet
하나의 belong to에 사람 : 그래서Elixir Ecto : belongs_to 및 has_many로 마이그레이션을 작성하는 방법은 무엇입니까?
defmodule MyApp.Person do
use MyApp.Web, :model
alias MyApp.Pet
schema "persons" do
field :name, :string
has_many :pets, Pet
timestamps()
end
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [])
|> validate_required([])
end
end
및
defmodule MyApp.Pet do
use MyApp.Web, :model
alias MyApp.Person
schema "pets" do
field :name, :string
belongs_to :person, Person
timestamps()
end
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [])
|> validate_required([])
end
end
방법 그게 migration이라고 쓰나요?
defmodule Iloveproblems.Repo.Migrations.CreatePersonsAndPets do
use Ecto.Migration
def change do
create table(:persons) do
add :name, :string
# I don't know :(. The has_many stuff
timestamps()
end
create table(:pets) do
add :name, :string
# I don't know :(. The belongs_to stuff
timestamps()
end
end
end
나는 을 사용하고 있습니다.
미리 감사드립니다.
'add : person_id, references (: persons), null : false' 등의 방법으로 이동합니다. – JustMichael