Omitting records

Report a typo

Suppose you are working with the library.db SQLite database; below, you can see the database diagram:

library database

Your task is to create various records of books and insert them into the books table. However, you want to omit inserting default values in specific fields since you don't know each book's publication date and publisher id.

We have already created a slice of books for you to insert into the books table. Your task is to write the additional code required to omit to insert the DatePublished and PublisherID fields.

While omitting fields during record creation is not a common operation, you can check out the db.Omit() method in GORM's official documentation to learn how to omit inserting default values into specific fields.
Write a program in Go
package main

import (
"fmt"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"log"
"time"
)

// DO NOT modify the model declarations below!
type Author struct { // Model for the `authors` table
gorm.Model
Name string
Books []Book `gorm:"many2many:author_books"`
}

type Book struct { // Model for the `books` table
gorm.Model
Title string
DatePublished time.Time
PublisherID uint
Authors []Author `gorm:"many2many:author_books"`
}

type Publisher struct { // Model for the `publishers` table
gorm.Model
Name string
Books []Book
}

func main() {
db := createDB() // DO NOT delete this line! it creates the in-memory `library` database

books := []Book{
{Title: "The Go Programming Language"},
___

Create a free account to access the full topic