结构体

// UUID UUID
type UUID struct {
    Id string `gorm:"type:varchar(40);primary_key;" sql:"comment:'UUID'"`
}

type DomainCLB struct {
    UUID

    DomainID string `gorm:"column:domain_id;type:varchar(64);not null;index:idx_domain_clb,unique" json:"domain_id"`
    CLBUUID  string `gorm:"column:clb_uuid;type:varchar(64);not null;index:idx_domain_clb,unique" json:"clb_uuid"`
    CLBID    string `gorm:"column:clb_id;type:varchar(64);not null;index:idx_domain_clb,unique" json:"clb_id"`
    CLBName  string `gorm:"column:clb_name;type:varchar(128);not null" json:"clb_name"`
    VIP      string `gorm:"column:vip;type:varchar(64);not null" json:"vip"`
    Region   string `gorm:"column:region;type:varchar(64);not null" json:"region"`
    Operator string `gorm:"column:operator;type:varchar(64);not null" json:"operator"`

    UpdatedAt time.Time `json:"updated_at" sql:"comment:'最后更新时间'"`
    CreatedAt time.Time `json:"created_at" sql:"comment:'创建时间'"`
}

注册Callback

instance.Callback().Create().Before("gorm:create").Register("before_create_v2", func(db *gormv2.DB) {
    if db.Statement.Schema != nil {
        db.Statement.SetColumn("Id", uuid.New().String())
    } else {
        log.Println("gormv2 callback: schema is nil, cannot set Id")
    }
})

调用批量插入

func foobar() error {
    var domainClbList []*dbmodels.DomainCLB
    for _, clb := range accClbList {
        domainClbList = append(domainClbList, &dbmodels.DomainCLB{
            DomainID: domainId,
            CLBUUID:  clb.Id,
            CLBID:    clb.ClbId,
            CLBName:  clb.ClbName,
            VIP:      clb.Vip,
            Region:   clb.CRegion,
            Operator: clb.Operator,
        })
    }

    ...

    return d.txv2.WithContext(ctx).Model(dbmodels.DomainCLB{}).CreateInBatches(domainClbList, len(domainClbList)).Error
}

结果插入的语句为

INSERT INTO `t_domain_clbs` (
        `id`,
        `domain_id`,
        `clb_uuid`,
        `clb_id`,
        `clb_name`,
        `vip`,
        `region`,
        `operator`,
        `updated_at`,
        `created_at`
    )
VALUES (
        '98e2d711-de27-4138-82c1-95d933298e0b',
        'c1b6277b-9373-49a8-a341-b0526a360d4d',
        '57418e89-fd39-4e8b-a3d2-ae468d8c3374',
        'xxxxx',
        'test-xx-8iczh42p',
        'xxxxx',
        'ap-tianjin',
        'telecom',
        '2025-08-07 19:58:16.866',
        '2025-08-07 19:58:16.866'
    ),
(
        '',
        'c1b6277b-9373-49a8-a341-b0526a360d4d',
        '729ca8b6-a15a-446f-9fe3-c058aecabdaa',
        'xxxxx',
        'test-xx-01edxna0',
        'xxxxx',
        'ap-shenzhen',
        'telecom',
        '2025-08-07 19:58:16.866',
        '2025-08-07 19:58:16.866'
    )

可以看到最后一条数据的id有问题

GORM Playground Link

https://github.com/go-gorm/playground/pull/1

Description

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking