Computer scienceProgramming languagesGolangWorking with dataRelational databasesGORM

Relationships between models

Declaring the Actor and Movie models

Report a typo

Quentin wants to create a new in-memory SQLite database that contains the actors and movies tables and set up a many-to-many relationship between them.

The actors table should have the following columns:

actors
id INTEGER, PK
created_at DATETIME
updated_at DATETIME
deleted_at DATETIME
first_name TEXT
last_name TEXT
birth_date DATETIME

The movies table should have the following columns:

movies
id INTEGER, PK
created_at DATETIME
updated_at DATETIME
deleted_at DATETIME
title TEXT
release_date DATETIME

And the actor_movies join table (which is automatically created by GORM) should have the following columns:

actor_movies
actor_id INTEGER, FK(actors)
movie_id INTEGER, FK(movies)

Your task is to help Quentin write the required code to declare the Actor and Movie models, using the gorm.Model struct, and use the `gorm:"many2many:..."` struct tag to set up the many-to-many relationship between Actor and Movie and create the actor_movies join table.

This problem uses the reflect package and the hidden function checkModels() to check if you correctly declared the Actor and Movie models and set up a many-to-many relationship between them.
Write a program in Go
package main

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

// Declare the `Actor` model below:
type Actor struct {
gorm.Model
FirstName ?
? ?
BirthDate time.Time
? []Movie `gorm:"?:actor_movies"`
}

// Declare the `Movie` model below:
type Movie struct {
?
? ?
ReleaseDate ?
Actors []? `gorm:"many2many:?"`
}

// DO NOT delete or modify the code within the main() function!
func main() {
_ = fmt.Print; _ = log.Print; _ = gorm.Config{}; _ = sqlite.Dialector{}
_ = reflect.Struct; _ = strings.ToLower; _ = unicode.IsUpper; _ = time.Time{}

checkModels()
___

Create a free account to access the full topic