[beego]Bug:ORM创建表的时候,不同的组合方式影响表的创建结果

2023-12-15 279 views
9
  1. 您使用的是哪个版本的 Go 和 beego (bee version)?
  1. 您使用什么操作系统和处理器架构 (go env)?
  1. 你做了什么?
func init() {
    // 获取数据库配置
    mysqlurls, _ := config.String("mysqlurls")
    mysqlport, _ := config.String("mysqlport")
    mysqluser, _ := config.String("mysqluser")
    mysqlpass, _ := config.String("mysqlpass")
    mysqldb, _ := config.String("mysqldb")
    debug, _ := config.Bool("debug")
    maxIdle, _ := config.Int("maxIdle") // 设置最大空闲连接
    maxConn, _ := config.Int("maxConn") // 设置最大数据库连接

    // 注册数据库服务
    // 参数1        数据库的别名,用来在 ORM 中切换数据库使用
    // 参数2        driverName
    // 参数3        对应的链接字符串
    // 参数4(可选)  设置最大空闲连接
    // 参数5(可选)  设置最大数据库连接 (go >= 1.2)
    orm.RegisterDataBase("default", "mysql", mysqluser+":"+mysqlpass+"@tcp("+mysqlurls+":"+mysqlport+")/"+mysqldb+"?charset=utf8&loc=Local", orm.MaxIdleConnections(maxIdle), orm.MaxOpenConnections(maxConn))

    // 是否开启 debug 模式
    orm.Debug = debug

    // 注册model
    // 建议一次注册所有的 model
    orm.RegisterModel(new(Tag), new(Comment), new(SimpleTable), new(Content))

    // 生成表
    // 参数1      数据库别名,一般为default;
    // 参数2      是否开启创建创建表(!!!如果值为ture时,表已经存在并且表中有值的情况下,它会先删除我们的表,然后重新创建)
    // 参数3      是否显示创建SQL语句
    orm.RunSyncdb("default", true, true)
}

// SimpleTable Model Struct
type SimpleTable struct {
    //Id   int
    Uid int `orm:"pk;auto"` // pk;auto 设置主键自增
    // Pk int `orm:"pk"` // pk 设置主键
    Name     string    `orm:"null;size(100)"`                  // size 设置字段长度
    Add      string    `orm:"column(address)"`                 // column 为字段设置db字段的名称
    Index    string    `orm:"index"`                           // index 为单个字段添加索引
    Unique   string    `orm:"unique"`                          // unique 为单个字段增加 unique 键
    Money    float64   `orm:"digits(12);decimals(2)"`          //digits 总长度12,decimals小数点后2位
    Status   int       `orm:"default(1);description:(注释状态字段)"` // default 为字段设置默认值 description为字段添加注释
    AnyField string    `orm:"-"`                               // -可忽略struct中的字段
    Created  time.Time `orm:"auto_now_add;type(datetime)"`     // auto_now每次model保存时都会对时间自动更新  type设置为date时,time.Time 字段的对应 db 类型使用 date
    Updated  time.Time `orm:"auto_now;type(datetime)"`         // auto_now_add第一次保存时才设置时间
}

type Content struct {
    Id   int
    TagId int `orm:"size(4)"`
    Title string `orm:"index;size(1000)"`
    Content string `orm:"null;type(text)"`
    Score float64 `orm:"default(10);digits(4);decimals(2);description:(评价分数)"`
    CreatedAt time.Time `orm:"auto_now_add;type(datetime)"`
    UpdatedAt time.Time `orm:"auto_now;type(datetime)"`
}

type Tag struct {
    TagId  int `orm:"pk;auto"`
    Tag string
    CreatedAt time.Time `orm:"auto_now_add;type(datetime)"`
}

type Comment struct {
    Id   int
    UserId int
    ContentId int
    Comment string
    CreatedAt time.Time `orm:"auto_now_add;type(datetime)"`
}
  1. 您希望看到什么?

顺利创建orm模型

  1. 你看到了什么?

现在的问题是,在不同的写法中,表不能顺利创建

orm.RegisterModel(new(标签), new(注释), new(SimpleTable), new(内容))

写入方法是可以顺利创建的,如果我调整了这样的顺序,则无法顺利创建。

// bug:只创建了 SimpleTable 和 Content
orm.RegisterModel(new(SimpleTable), new(Content),new(Tag), new(Comment))

// bug:只创建了 new(SimpleTable), new(Comment), new(Content)
orm.RegisterModel(new(SimpleTable), new(Comment), new(Content),new(Tag))

// bug: 只创建了 new(SimpleTable),new(Comment),new(Content)
orm.RegisterModel(new(SimpleTable),new(Comment),new(Content), new(Tag))

// 全部创建,没有问题
orm.RegisterModel(new(Tag),new(SimpleTable), new(Comment), new(Content))

回答

7

您使用了错误的类型。当要在longtext类型的列上创建索引时,必须指定索引的长度。

9

我认为这个应该属于一个bug,因为这种情况下beego并没有给开发者提示。项目中在数据表中非常多的情况下,开发者不可能一对照,所以非常容易出错。希望开发者可以支持者表没有生成的情况下会给出错误警告!