User and Role GORM models

Report a typo

Emma wants to create a simple Go program to manage active and inactive user accounts. She needs to create a new in-memory SQLite database with the users and roles tables.

The users table should have the following columns:

users
id INTEGER
created_at DATETIME
updated_at DATETIME
deleted_at DATETIME
name TEXT UNIQUE NOT NULL
email TEXT INDEX
is_active BOOL

And the roles table should have the following columns:

roles
id INTEGER
created_at DATETIME
updated_at DATETIME
deleted_at DATETIME
name TEXT UNIQUE NOT NULL
description TEXT

Your task is to help Emma write the required code to declare the User and Role models, using the gorm.Model struct. Apart from that, use appropriate GORM struct tags to ensure the special constraints that the name and email columns should have.

This problem uses the reflect package and the hidden function checkModels() to check if you correctly declared the User and Role models.
Write a program in Go
package main

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

// Declare the `User` model below:
type ? struct {
?
? ? `gorm:"unique;not null"`
Email ? ?
IsActive ?
}

// Declare the `Role` model below:
type ? struct {
gorm.Model
Name string ?
? ?
}

// 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;

checkModels()
}
___

Create a free account to access the full topic