程序员的资源宝库

网站首页 > gitee 正文

beego migrate 使用记录 beego demo

sanyeah 2024-03-29 16:19:12 gitee 7 ℃ 0 评论

1、生成迁移文件

// 命令格式
bee generate migration [migrationfile] [-fields="name:type"]

// 示例
bee generate migration user

2、完成迁移脚本

// Run the migrations
func (m *User_20220406_155638) Up() {
	m.CreateTable("user", "innodb", "utf8mb4")
	m.PriCol("id").SetAuto(true).SetDataType("int(11)").SetUnsigned(true)
	m.NewCol("nickname").SetDataType("varchar(255)").SetNullable(false)
	m.NewCol("avatar").SetDataType("varchar(32)").SetDefault("''").SetNullable(false)
	m.NewCol("status").SetDataType("tinyint(1)").SetUnsigned(true).SetDefault("1").SetNullable(false)
	m.NewCol("created_at").SetDataType("int(11)").SetUnsigned(true).SetNullable(false)
	m.NewCol("updated_at").SetDataType("int(11)").SetUnsigned(true).SetNullable(false)
	m.SQL(m.GetSQL())
}

// Reverse the migrations
func (m *User_20220406_155638) Down() {
	m.SQL("DROP TABLE IF EXISTS user")
}

此处注意默认空字符的写法,SetDefault("''")

执行迁移命令

// 命令格式
bee migrate [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"] [-dir="path/to/migration"]

// 示例
bee migrate -conn="root:password@tcp(127.0.0.1:3306)/database"

默认是mysql数据库,无需添加参数,默认路径也无需修改

出现类似的一下提示,missing go.sum entry for module providing package,请执行

go mod tidy

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表