Creating your first record

Report a typo

Suppose you are working with the music SQLite database, below you can see the database diagram:

music database diagram

Your task is to create a single record for the artists table; you must create the artist named "Elvis Presley".

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 Artist struct { // Model for the `artists` table
gorm.Model
Name string
Songs []Song `gorm:"many2many:artist_songs"`
}

type Song struct { // Model for the `songs` table
gorm.Model
Title string
Duration time.Duration
Artists []Artist `gorm:"many2many:artist_songs"`
}

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

// Create the Artist named "Elvis Presley" below:
artist := ?
result := db.?(?)
if result.Error != nil {
log.Fatalf("cannot create Artist: %v\n", result.Error)
}

// DO NOT delete the line below! it checks if the Artist record was created
checkCreatedRecord(db, &artist)
___

Create a free account to access the full topic