Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support the AUTO_RANDOM Field #5981

Closed
wants to merge 3 commits into from

Conversation

Icemap
Copy link
Contributor

@Icemap Icemap commented Jan 13, 2023

  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

For this issue: #5977
Support the AUTO_RANDOM.

User Case Description

If you already deploy a TiDB at localhost:4000. You can use this code to test.

package main

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

type Product struct {
	ID    uint `gorm:"primaryKey;autoRandom:true"`
	Code  string
	Price uint
}

func main() {
	db, err := gorm.Open(mysql.Open("root:@tcp(127.0.0.1:4000)/test"), &gorm.Config{})
	if err != nil {
		panic("failed to connect database")
	}

	db.AutoMigrate(&Product{})

	insertProduct := &Product{Code: "D42", Price: 100}

	db.Create(insertProduct)
	fmt.Printf("insert ID: %d, Code: %s, Prict: %d\n",
		insertProduct.ID, insertProduct.Code, insertProduct.Price)

	readProduct := &Product{}
	db.First(&readProduct, "code = ?", "D42") // find product with code D42

	fmt.Printf("read ID: %d, Code: %s, Prict: %d\n",
		readProduct.ID, readProduct.Code, readProduct.Price)
}

@Icemap
Copy link
Contributor Author

Icemap commented Jan 16, 2023

@jinzhu PTAL

@Icemap
Copy link
Contributor Author

Icemap commented Jan 29, 2023

Using new PR for avoiding interface changes.

@Icemap Icemap closed this Jan 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant