CtrlK
CommunityDocumentationLog inGet started
Tessl Logo

tessl/golang-gorm-io--gorm

tessl install tessl/golang-gorm-io--gorm@1.31.0

Full-featured ORM library for Golang with associations, hooks, transactions, migrations, and developer-friendly chainable API

Agent Success

Agent success rate when using this tile

81%

Improvement

Agent success rate improvement when using this tile compared to baseline

1.13x

Baseline

Agent success rate without this tile

72%

task.mdevals/scenario-4/

Book Management System

Build a book management system that tracks books and their authors using a relational database structure.

Requirements

Implement a system with two models: Book and Author. Each book belongs to an author, and an author can have multiple books.

Data Models

Create two models with the following specifications:

Author Model:

  • ID (auto-incrementing primary key)
  • Name (string)
  • Country (string)
  • Standard timestamp fields

Book Model:

  • ID (auto-incrementing primary key)
  • Title (string)
  • ISBN (string)
  • Author relationship (each book belongs to one author)
  • Standard timestamp fields

Core Functionality

Implement the following operations in a main.go file:

  1. Database Setup: Initialize an in-memory SQLite database and run auto-migration for both models.

  2. Create Data: Insert sample data into the database:

    • Create an author named "Jane Smith" from "USA"
    • Create two books for this author: "The Great Adventure" (ISBN: "978-0-1234-5678-9") and "Mystery Island" (ISBN: "978-0-9876-5432-1")
  3. Query with Preloading: Retrieve a book by its title and preload its associated author information. Print the book title and the author's name.

  4. Query Books by Author: Find all books written by a specific author. Print each book's title.

  5. Update Author: Change the country of an author and verify the update was successful.

Test Cases

  • When querying a book and preloading the author, the author's information is correctly populated. @test
  • When querying books by author, all books belonging to that author are returned. @test
  • When updating an author's information, the changes are reflected in the database. @test

Implementation

@generates

API

package main

import (
	"gorm.io/gorm"
)

// Author represents an author in the system
type Author struct {
	ID      uint
	Name    string
	Country string
	CreatedAt time.Time
	UpdatedAt time.Time
}

// Book represents a book in the system
type Book struct {
	ID        uint
	Title     string
	ISBN      string
	AuthorID  uint
	Author    Author
	CreatedAt time.Time
	UpdatedAt time.Time
}

func main() {
	// Implementation here
}

Dependencies { .dependencies }

gorm { .dependency }

Provides ORM functionality for database operations.

gorm.io/driver/sqlite { .dependency }

Provides SQLite database driver for GORM.

Version

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
golangpkg:golang/gorm.io/gorm@v1.31.1
tile.json