"row value misused" from sqlite/gorm

I want to relate a model to another model Like this

1. Quiz
|
|----- Question
______Question

2. Quiz
|
|----- Question
______Question

Currently I am storing the Quiz in a table and Question in another table. each Question have reference to its Quiz by their QuizID.

Code:-


// models
type Question struct {
    gorm.Model
    QuizID       uint     `json:"-"`
    Text         string   `json:"text" gorm:"not null"`
    Options      []string `json:"options" gorm:"not null;type:text[]"`
    AnswerOption string   `json:"answerOption" gorm:"not null"`
}

type Quiz struct {
    gorm.Model
    Title     string     `json:"title" gorm:"not null"`
    CreatedBy string     `json:"createdBy" gorm:"not null"`
    Questions []Question `json:"questions" gorm:"foreignKey:QuizID;not null"`
}

// post request handler
func CreateQuiz(context *gin.Context) {
    var quiz models.Quiz
    if err := context.ShouldBindJSON(&quiz); err != nil {
        context.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
        return
    }

    record := database.Instance.Create(&quiz)  // error throws at here
    if record.Error != nil {
        context.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": record.Error.Error()})
        return
    }

    context.JSON(http.StatusCreated, gin.H{"message": "Created successfully"})
}

I am using python httpx as client

import httpx


url = "http://localhost:8080/api/create"

q = {
    "title": "quiz title",
    "createdBy": "a user",
    "questions": [
        {
            "text": "your name?",
            "options": ["a", "b", "c", "d"],
            "answerOption": "a",
        }
    ],
}

resp = httpx.post(url,
                  json=q,
                  )
print(resp.status_code)
print(resp.text)

This is the response from the python code

kulothungan@lenovo:~/quiz-maker-backend$ /bin/python3 /home/kulothungan/main.py
500
{"error":"row value misused"}

Response from go:-


2023/08/07 14:04:18 /home/kulothungan/quiz-maker-backend/controllers/quiz_controller.go:18 row value misused
[0.197ms] [rows:0] INSERT INTO `questions` (`created_at`,`updated_at`,`deleted_at`,`quiz_id`,`text`,`options`,`answer_option`) VALUES ("2023-08-07 14:04:18.925","2023-08-07 14:04:18.925",NULL,2,"your name?",("a","b","c","d"),"a") ON CONFLICT (`id`) DO UPDATE SET `quiz_id`=`excluded`.`quiz_id` RETURNING `id`

2023/08/07 14:04:18 /home/kulothungan/quiz-maker-backend/controllers/quiz_controller.go:18 row value misused
[1.419ms] [rows:1] INSERT INTO `quizzes` (`created_at`,`updated_at`,`deleted_at`,`title`,`created_by`) VALUES ("2023-08-07 14:04:18.924","2023-08-07 14:04:18.924",NULL,"quiz title","a user") RETURNING `id`
[GIN] 2023/08/07 - 14:04:18 | 500 |    1.953792ms |       127.0.0.1 | POST     "/api/create"

Please help me to fix this issue, I have been trying to fix it for 3 days.

Comment From: keyvangholami

SQLite doesn't support array types natively, so using a text[] type for the Options field in your Question model can cause this error.

Instead of trying to store the Options as an array, you can serialize them as a JSON string or a comma-separated string.

Comment From: kulothunganug

SQLite doesn't support array types natively

That's something I totally forgot... 😓

I found https://github.com/go-gorm/datatypes

Modified Question that works nice

import (
    "gorm.io/datatypes"
    "gorm.io/gorm"
)

type Question struct {
    gorm.Model
    QuizID       uint                        `json:"-"`
    Text         string                      `json:"text" gorm:"not null"`
    Options      datatypes.JSONSlice[string] `json:"options" gorm:"not null"`
    AnswerOption string                      `json:"answerOption" gorm:"not null"`
}

Thank you very much for helping out 😃