gorm的一些注意点

2022/4/27 23:13:42

本文主要是介绍gorm的一些注意点,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.create id列未赋值最新的id 

检查id字段是否贴有  `gorm:"column:draftsId;primary_key;type:int(11);not null"`

2.create 测试环境能带出id值,预发环境Create 一直返回0 

测试环境db表格带有主键,预发环境db表格未建主键自增

3.使用动态构建查询

//conditionGetList 动态构建查询条件
func conditionGetList(tableName string, info *model.TClientZybDrafts) *gorm.DB {
    query := dao.InnerDao.Gorm.Table(tableName)
    if info.ClientID > 0 {
        query = query.Where("clientId = ?", info.ClientID)
    }
    if info.AcademicYear > 0 {
        query = query.Where("academicYear = ?", info.AcademicYear)
    }
    if info.Wenli > 0 {
        query = query.Where("wenli = ?", info.Wenli)
    }
    if info.DiplomaID > 0 {
        query = query.Where("diplomaId = ?", info.DiplomaID)
    }
    if info.Batch > 0 {
        query = query.Where("batch = ?", info.Batch)
    }
    if info.EnrollStage > 0 {
        query = query.Where("enrollStage = ?", info.EnrollStage)
    }
    if info.EnrollMode > 0 {
        query = query.Where("enrollMode = ?", info.EnrollMode)
    }
    if info.EnrollCategory > 0 {
        query = query.Where("enrollCategory = ?", info.EnrollCategory)
    }
    if info.BatchEx > 0 {
        query = query.Where("batchEx = ?", info.BatchEx)
    }
    return query
}

//使用
sqlErr := conditionGetList(TableClientZybDrafts(orgId), info).Order(" draftsId Desc").Find(&m).Error

 



这篇关于gorm的一些注意点的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程