Hello all, I am new to Prisma, and can use some he...
# madewithprisma
c
Hello all, I am new to Prisma, and can use some help with connecting nested data (array), is anyone a Prisma expert/ guru in here to help?
🙌 1
v
Hello @Chris Bitoy! I would be glad to help you, I am a Prisma Ambassador
have you posted a question on stackoverflow yet?
c
Oh thank you so much. No I haven’t posted yet on stackoverflow - figured I’ll get better luck on here 🙂
Please bare with my long message, I wanted to make sure you have full understanding of the issue I am having…
v
No problem! Although, I would recommend posting on stackoverflow so that more people can learn from this 🙂 Either way, just let me know when the details are ready
c
In my app I have a dropdown list that has pre-defined questions and answers. Some of the questions have unique answers, and some share answers with other questions. Example: Question one -- Answer one -- Answer two -- Answer three Question two -- Answer one -- Answer four -- Answer three Issue I am having is that I tried pairing (Via Prisma Studio) questions with answers like example shown above, but its seems that if I pair a question with an answer, I can’t pair the same answer with another question. Please see this video https://jumpshare.com/v/nQIomRynUPLXeDblNAOX My table model was set so that one question can take many answers - please see codes below:
Copy code
model Template {
  id    Int     @id @default(autoincrement())
  title String
  email String? @unique

  // Relation fields
  questions Question[]
  answers   Answer[]
}

model Question {
  id              Int      @id @default(autoincrement())
  owner_id        String?  @default(cuid())
  campaign_id     String?  @default(cuid())
  freestyle_order Int?     @unique
  checked         Boolean  @default(false)
  input_type      String   @default("radio")
  ui_section      Int?     @default(3)
  heading         String?
  child_questions String   @db.VarChar(500)
  answer_type     Int?
  correct_answer  String?
  point_value     Int?
  createdAt       DateTime @default(now())
  updatedAt       DateTime @updatedAt

  // Relation fields
  answers    Answer[]
  Template   Template? @relation(fields: [templateId], references: [id])
  templateId Int?
}

model Answer {
  id         Int       @id @default(autoincrement())
  options    String
  question   Question? @relation(fields: [questionId], references: [id])
  questionId Int?
  template   Template? @relation(fields: [templateId], references: [id])
  templateId Int?
  createdAt  DateTime  @default(now())
  updatedAt  DateTime  @updatedAt
}
My routes look like this:
Copy code
// Template endpoint

//Get all
router.get("/templates", async (req, res) => {
  try {
    const templates = await prisma.template.findMany({
      include: {
        questions: {
          include: {
            answers: true,
          },
        },
      },
    });
    res.status(200).send({ data: templates, error: "", status: 200 });
  } catch (error) {
    console.log(error);
    res.status(500).send({ data: {}, error: error, status: 500 });
  }
});

//Create a template
<http://router.post|router.post>("/templates", async (req, res) => {
  try {
    const { title, email, id } = req.body;
    const template = await prisma.template.create({
      data: {
        title,
        email,
      },
      answers: {
        connect: {
          id: id,
        },
      },
    });
    res.status(200).send({ data: template, error: "", status: 200 });
  } catch (error) {
    console.log(error);
    res.status(500).send({ data: {}, error: error, status: 500 });
  }
});
Copy code
//Questions endpoint

// Get all questions
router.get("/questions", async (req, res) => {
  try {
    const questions = await prisma.question.findMany({
      include: {
        Template: true,
        answers: true,
      },
    });
    res.status(200).send({ data: questions, error: "", status: 200 });
  } catch (error) {
    console.log(error);
    res.status(500).send({ data: {}, error: error, status: 500 });
  }
});


//Create a question
<http://router.post|router.post>("/questions", async (req, res) => {
  const { child_questions, checked, input_type, answerID } = req.body;
  try {
    const question = await prisma.question.create({
      data: {
        child_questions,
        checked,
        input_type,
        answers: {
          connect: {
            id: id,
          },
        },
      },
    });
    res.status(200).send({ data: question, error: "", status: 200 });
  } catch (error) {
    console.log(error);
    res.status(500).send({ data: {}, error: error, status: 500 });
  }
});
Copy code
//Answers endpoint

// Get all answers
router.get("/answers", async (req, res) => {
  try {
    const answers = await prisma.answer.findMany({
      include: {
        question: true,
      },
    });
    res.status(200).send({ data: answers, error: "", status: 200 });
  } catch (error) {
    console.log(error);
    res.status(500).send({ data: {}, error: error, status: 500 });
  }
});


//Create a answer
<http://router.post|router.post>("/answers", async (req, res) => {
  const { option } = req.body;
  try {
    const answers = await prisma.answer.create({
      data: {
        option,
      },
      question: {
        connect: {
          id: id,
        },
      },
      template: {
        connect: {
          id: id,
        },
      },
    });
    res.status(200).send({ data: answers, error: "", status: 200 });
  } catch (error) {
    console.log(error);
    res.status(500).send({ data: {}, error: error, status: 500 });
  }
});
// Current state of Data Structure : http://3.215.90.93:5000/api/templates
Copy code
// Desired/expected data structure 

const questions = [
  {
    id: 1,
    title: "Top 5 Questions",
    email: "<mailto:me@myemail.com|me@myemail.com>",
    questions: [
      {
        question:
          "What features are most important to you when considering a new snack brand?",
        checked: false,
        input_type: "radio",
        answers: [
          "Heard of it - never tried it before",
          "Tried it once before",
          "Buy it regularly",
          "Curious to learn more",
        ],
      },
      {
        question:
          "In what time frame are you considering buying a new snack brand?",
        input_type: "radio",
        checked: false,
        answers: [
          "Never heard of it",
          "Heard of it - never tried it before",
          "Tried it once before",
          "Buy it regularly",
          "Curious to learn more",
        ],
      },
      {
        question: "How familiar are you with Zoey & Eva's Cookies?",
        input_type: "radio",
        checked: false,
        answers: [
          "Never heard of it",
          "Heard of it - never tried it before",
          "Tried it once before",
          "Buy it regularly",
          "Curious to learn more",
        ],
      },
      {
        question:
          "How likely are you to recommend or discuss Zoey & Eva's Cookies with a friend?",
        input_type: "radio",
        checked: false,
        answers: [
          "Never heard of it",
          "Heard of it - never tried it before",
          "Tried it once before",
          "Buy it regularly",
          "Curious to learn more",
        ],
      },
    ],
  },
  {
    id: 2,
    title: "Brand Questions",
    questions: [
      {
        question: "How familiar are you with Zoey & Eva's Cookies?",
        input_type: "radio",
        checked: false,
        answers: [
          "Never heard of it",
          "Heard of it - never tried it before",
          "Tried it once before",
          "Buy it regularly",
          "Curious to learn more",
        ],
      },
      {
        question:
          "How likely are you to recommend or talk with friends about Zoey & Eva's Cookies in the next 30 days?",
        input_type: "radio",
        checked: false,
        answers: [
          "Never heard of it",
          "Heard of it - never tried it before",
          "Tried it once before",
          "Buy it regularly",
          "Curious to learn more",
        ],
      },
      {
        question:
          "Which of the following words would you associate with Zoey & Eva's Cookies?",
        input_type: "radio",
        checked: false,
        answers: [
          "Never heard of it",
          "Heard of it - never tried it before",
          "Tried it once before",
          "Buy it regularly",
          "Curious to learn more",
        ],
      },
    ],
  },
]
Thank you in advance
v
From a general review… The first thing I notice is that you currently have “question Question?” on the Answer model, which is wrong if you want to have a many-to-many relationship. On both sides you need a list
c
I see.
I was trying to create a one-to-many relationships - one question has many answers
So that would create a list of answers to map with a question (drop down). This is the actual app: http://attenx-client.s3-website-us-east-1.amazonaws.com/questions
How would you approach this, how would you do it ?