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 #104

Merged

Conversation

Icemap
Copy link
Contributor

@Icemap Icemap commented Jan 29, 2023

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

What did this pull request do?

For this issue: go-gorm/gorm#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;default:auto_random()"`
	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 Icemap changed the title feat: support auto_random through default tag Support the AUTO_RANDOM Field Jan 29, 2023
@Icemap
Copy link
Contributor Author

Icemap commented Jan 29, 2023

Change these two PR's implementation methods:

For avoiding interface changes.

@Icemap
Copy link
Contributor Author

Icemap commented Jan 29, 2023

PTAL @jinzhu

mysql.go Outdated
func autoRandomType(field *schema.Field) (bool, string) {
if field.PrimaryKey && field.HasDefaultValue &&
strings.ToLower(strings.TrimSpace(field.DefaultValue)) == AutoRandomTag {
field.HasDefaultValue = false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to set HasDefaultValue to false, it should be true?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We used this tag default:auto_random() to implement the auto_random feature. But it is actually not a default value. It's a field like auto_increment.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for auto_increment fields we also marked them as HasDefaultValue, e.g:

https://github.com/go-gorm/gorm/blob/d834dd60b715422dc2a900fb2744f9c278a9830f/schema/field.go#L113

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, got it. I made a mistake.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. PTAL again.

@jinzhu jinzhu merged commit e1a37d1 into go-gorm:master Jan 31, 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.

2 participants