github.com/quickfeed/quickfeed@v0.0.0-20240507093252-ed8ca812a09c/doc/gorm-issues.md (about)

     1  # Note about GORM database issues
     2  
     3  When using GORM, we must be careful when using false or zero values.
     4  This is because GORM will ignore false or zero values, and will not produce the expected SQL query.
     5  Here are some examples of different ways that may produce unexpected SQL queries:
     6  
     7  ```go
     8  a.Where(&qf.Assignment{IsGroupLab: false}) // Bad: the false value will simply be ignored
     9  a.Where("is_group_lab = ?", "false")       // Bad: the value will be a string, not a bool
    10  a.Where("is_group_lab = ?", false)         // Good: the value will be a bool
    11  a.Where("is_group_lab = false")            // Good: the value will be a bool (same as above)
    12  a.Where(&qf.Assignment{IsGroupLab: true})  // Good: the true value will be used
    13  ```
    14  
    15  The following SQL queries are produced by the above examples:
    16  
    17  ```sql
    18  SELECT `id` FROM `assignments` WHERE `assignments`.`course_id` = 1 ORDER BY 'order'
    19  SELECT `id` FROM `assignments` WHERE `assignments`.`course_id` = 1 AND is_group_lab = \"false\" ORDER BY 'order'
    20  SELECT `id` FROM `assignments` WHERE `assignments`.`course_id` = 1 AND is_group_lab = false ORDER BY 'order'
    21  SELECT `id` FROM `assignments` WHERE `assignments`.`course_id` = 1 AND is_group_lab = false ORDER BY 'order'
    22  SELECT `id` FROM `assignments` WHERE `assignments`.`course_id` = 1 AND `assignments`.`is_group_lab` = true ORDER BY 'order'
    23  ```
    24  
    25  ## Debugging GORM issues
    26  
    27  To debug a particular database issue, you can use the following environment variables to enable GORM logging:
    28  
    29  ```bash
    30  LOG=1 LOGDB=4 go test -v -run TestGetSubmissionsByCourse
    31  ```
    32  
    33  This will generate a lot of output, but you should be able to find the particular SQL query that is being generated for a particular GORM query.