Skip to content

Running second time AutoMigrate fails #120

@gsbingo17

Description

@gsbingo17

The AutoMigrate method on already existing entities does not deal with unique index and the nullable column accurately.

  • unique index
package main

import (
	spannergorm "github.com/googleapis/go-gorm-spanner"
	"gorm.io/gorm"

	"gorm.io/gorm/logger"
)

type Apple struct {
	AppleID string `gorm:"primaryKey"`
	Name    string `gorm:"not null;index:idx_name_email,unique"`
	Email   string `gorm:"not null;index:idx_name_email,unique"`
	Age     int
}

func main() {
	// Replace with your actual Cloud Spanner project, instance, and database
	dsn := "projects/your-project-id/instances/your-instance/databases/your-db"

	db, err := gorm.Open(spannergorm.New(spannergorm.Config{
		DriverName: "spanner",
		DSN:        dsn,
	}), &gorm.Config{
		Logger: logger.Default.LogMode(logger.Info),
	})

	if err != nil {
		panic("failed to connect database")
	}

	// AutoMigrate will create the table with the unique indexes
	db.AutoMigrate(&Apple{})
}

Please refer to the following error:

rpc error: code = NotFound desc = uni_apples_name is not a constraint in apples
  • nullable column
type Admin struct {
   Id            int64      `gorm:"column:id;primarykey;not null;comment:自增ID"`
   LastLoginTime *time.Time `gorm:"column:last_login_time;null;default:null`
}

Please refer to the following error:

ALTER TABLE `user_admin` ALTER COLUMN `last_login_time` TIMESTAMP DEFAULT (null)

// Add this column again, it is not expected behavior

Environment details

  • Programming language: Golang
  • OS: MacBook
  • Language runtime version: 1.23
  • Package version: 1.4.0

Steps to reproduce

  1. Execute the sample code;
  2. Run it again;
  3. Change the sample code to use nullable column, and test it again

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions