GORM version 2 changes

Preface

In August 2020, GORM v2.0 was released. There are a lot of changes and some functions aren't compatible with v1, so I'd like to cover them in this article. change log link

Change Log 
v2.0 - 2020.08
GORM 2.0 is a rewrite from scratch, introducing incompatible API changes and many improvements

-Performance improvement
-Improved modularity
- Context, Batch Insert, Prepared Statment Mode, DryRun Mode, Join Preload, Find To Map, Create From Map,Supports FindInBatches.
- SavePoint/RollbackTo/Supports Nested Transaction.
-Named arguments, group conditions, upserts, locks, optimizers/index/Support for comment hints, improved subqueries
-Full self-reference relationship support, improved join table, batch data association mode
-Create/Support for multiple fields for tracking update times. UNIX (millimeter)/Adds support for nanoseconds.
-Field permission support: read-only, write-only, create-only, update-only, ignored
-New Plugin System: Multiple Databases, Read by Plugin Database Resolver/Support for split write, integration of Prometheus ...
-New Hook API: Integrated interface with plugins
-New migration: relationships, constraints/You can create database foreign keys for checker support, extended index support
-New logger: contextual support, increased extensibility
-Unified naming rules: table name, field name, join table name, foreign key, checker, index name
-Support for better customized data types(e.g: JSON)

The incompatible parts due to version differences are described below.

Database connection

v2

main.go


package main

import (
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

func main(){
	dsn := "root:@tcp(localhost:3306)/gorm_project?charset=utf8&parseTime=True&loc=Local"
	v2_db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
}

v1

main.go


package main

import (
      "github.com/jinzhu/gorm"
      _"github.com/jinzhu/gorm/dialects/mysql"
)

func main(){
	v1_db, err := gorm.Open("mysql", "root:@tcp(localhost:3306)/gorm_project?charset=utf8&parseTime=True&loc=Local")
}

Close database

v2

main.go


package main

import (
    "gorm.io/driver/mysql"
    "gorm.io/gorm"
)

func main(){
	dsn := "root:@tcp(localhost:3306)/gorm_project?charset=utf8&parseTime=True&loc=Local"
	v2_db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	}
	db, err := v2_db.DB()
	defer db.Close()
}

v1

main.go



package main

import (
        "github.com/jinzhu/gorm"
      _"github.com/jinzhu/gorm/dialects/mysql"
)

func main(){
    v1_db, err := gorm.Open("mysql", "root:@tcp(localhost:3306)/gorm_project?charset=utf8&parseTime=True&loc=Local")
    defer v1_db.Close()
}

New table

v2

main.go


package main

import (
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

type User struct {
  	Id int
	Name string
	Age int
	Addr string
	Pic string
}

func main(){
  dsn := "root:@tcp(localhost:3306)/gorm_project?charset=utf8&parseTime=True&loc=Local"
  v2_db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
  //Create a table called Users
  v2_db.Migrator().CreateTable(&User{})
}

v1

main.go


package main

import (
      "github.com/jinzhu/gorm"
      _"github.com/jinzhu/gorm/dialects/mysql"
)

func main(){
    v1_db, err := gorm.Open("mysql", "root:@tcp(localhost:3306)/gorm_project?charset=utf8&parseTime=True&loc=Local")
    //Create a table called Users
    v1_db.CreateTable(&User{})
}

Delete table

v2

main.go


package main

import (
    "gorm.io/driver/mysql"
    "gorm.io/gorm"
)

type User struct {
    Id int
    Name string
    Age int
    Addr string
    Pic string
}

func main(){
  dsn := "root:@tcp(localhost:3306)/gorm_project?charset=utf8&parseTime=True&loc=Local"
  v2_db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
  
  //Delete table
  v2_db.Migrator().DropTable(&User{})
}

v1

main.go


package main

import (
      "github.com/jinzhu/gorm"
      _"github.com/jinzhu/gorm/dialects/mysql"
)

func main(){
    v1_db, err := gorm.Open("mysql", "root:@tcp(localhost:3306)/gorm_project?charset=utf8&parseTime=True&loc=Local")
    //Delete table
    v1_db.DropTable("users")
}

Confirmation of table existence

v2

main.go


package main

import (
    "gorm.io/driver/mysql"
    "gorm.io/gorm"
)

func main(){
  dsn := "root:@tcp(localhost:3306)/gorm_project?charset=utf8&parseTime=True&loc=Local"
  v2_db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})

  //Confirmation of table existence
  b := v2_db.Migrator().HasTable("users")
  //The return value is a bool type
  fmt.Println(b)
}

v1

main.go



package main

import (
      "github.com/jinzhu/gorm"
      _"github.com/jinzhu/gorm/dialects/mysql"
)

func main(){
    v1_db, err := gorm.Open("mysql", "root:@tcp(localhost:3306)/gorm_project?charset=utf8&parseTime=True&loc=Local")
    //Confirmation of table existence
    v1_db.HasTable("users")
    //The return value is a bool type
    fmt.Println(b)
}

Discarding Related functions

Related functions cannot be used in v2.

// Related get related associations
func (s *DB) Related(value interface{}, foreignKeys ...string) *DB {
	return s.NewScope(s.Value).related(value, foreignKeys...).db
}

Change how to use LogMode

v2

//Specifying Log Level
db.Logger.LogMode(4)

v1

db.LogMode(Boolean value)

Recommended Posts

GORM version 2 changes