The songs database schema

Report a typo

Below you will see the songs database diagram:

songs database diagram

The songs database has the following relationship between tables:

  • artists — stores the name of all the artists;
  • albums — stores the title of the album, each artist may have one or many albums;
  • tracks — stores the name and duration of the tracks (songs), each album may have one or many tracks;

Your task is to create the Track, Album, and Artist models, and then use the db.AutoMigrate() function to create the schema of the songs sqlite3 database.

Write a program in Go
package main

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

// Create the 'Track' model below:
type Track struct {
gorm.Model
? string
? int
Album? uint
}

// Create the 'Album' model below:
type Album struct {
gorm.Model
Title string
? []Track
?ID uint
}

// Create the 'Artist' model below:
type Artist struct {
gorm.?
? string
Albums []?
}

func main() {
// DO NOT modify the gorm.Open code block, it connects to an in-memory database to create the 'songs' schema:
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
if err != nil {
log.Fatal(err)
}

// Write the code to migrate the schema conformed by 'Track', 'Album' and 'Artist' models:
err = db.?(?, ?, ?)
if err != nil {
log.Fatal(err)
}

// DO NOT delete the code line below, it checks if the tables exist after the migration:
checkTablesExists(db)
}
___

Create a free account to access the full topic