Issues with Creating Data Tables Using Structs in Golang + GORM

Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: golang+gorm使用结构体创建数据表问题

| username: TiDBer_4ZtoEU7K

The structure is as follows:

type Test struct {
		ID          uint64 `gorm:"primaryKey;auto_random;not null"`         
		Name        string `gorm:"column:name;size:100;not null"`     
		CreateAt    uint64  `gorm:"column:create_at;index"`          
		UpdateAt    uint64  `gorm:"column:update_at"`        
	}

After creating the table with the following statement, the AUTO_RANDOM for the id is not added. Is there any way to solve this?

db.AutoMigrate(&Test{})

| username: hey-hoho | Original post link

Gorm probably doesn’t support the auto_random tag, which is a TiDB custom tag. According to the documentation, TiDB can support changing from AUTO_INCREMENT to AUTO_RANDOM. Why not try using the autoIncrement tag first, and then modify it to auto_random using SQL?

| username: liuis | Original post link

GORM already supports TiDB, right? gorm:“primaryKey;default:auto_random()”