Created
June 22, 2017 15:56
-
-
Save Cifer-Y/70980b19deabed1491d694ba134cbcfd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Playground.Repo.Migrations.CreatePlayground.Psq.Answer do | |
use Ecto.Migration | |
def change do | |
create table(:psq_answers) do | |
add :survey_id, :integer | |
timestamps() | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Playground.Repo.Migrations.CreatePlayground.AnswerDetail do | |
use Ecto.Migration | |
def change do | |
create table(:psq_answer_details) do | |
add :question_id, :integer | |
add :psq_answer_id, references(:psq_answers) | |
add :question_type, :string | |
add :content, :string | |
timestamps() | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Playground.Psq.Answer do | |
use Ecto.Schema | |
import Ecto.Changeset | |
alias Playground.Psq.Answer | |
schema "psq_answers" do | |
field :survey_id, :integer | |
has_many :psq_answer_details, Playground.Psq.AnswerDetail | |
timestamps() | |
end | |
@doc false | |
def changeset(%Answer{} = answer, attrs) do | |
answer | |
|> cast(attrs, [:survey_id]) | |
|> validate_required([:survey_id]) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Playground.Psq.AnswerDetail do | |
use Ecto.Schema | |
import Ecto.Changeset | |
alias Playground.Psq.AnswerDetail | |
schema "psq_answer_details" do | |
field :content, :string | |
field :question_id, :integer | |
field :question_type, :string | |
belongs_to :psq_answer, Playground.Psq.Answer | |
timestamps() | |
end | |
@doc false | |
def changeset(%AnswerDetail{} = answer_detail, attrs) do | |
answer_detail | |
|> cast(attrs, [:question_id, :question_type, :content]) | |
|> validate_required([:question_id, :question_type]) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ecto.Query, warn: false | |
alias Playground.Repo | |
alias Playground.Psq.Answer | |
alias Playground.Psq.AnswerDetail | |
Repo.insert!(%Answer{survey_id: 1, psq_answer_details: [%AnswerDetail{question_id: 1, question_type: "fill", content: "fff"}]}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
in psq_answers table: | |
id | survey_id | |
1 | 1 | |
in psq_answer_details table: | |
id | question_id | psq_answer_id | question_type | content | |
1 | 1 | NULL | fill | fff | |
no psq_answer_id inserted |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment