開發與維運

使用 go-xorm/cmd/xorm reverse 遇到的一些問題

關於 xorm

xorm 是一組數據庫操作命令行工具。支持多種特性,具體功能移步至github/go/xorm

使用 golang 操作數據庫的同學都會遇到一個問題 —— 根據數據表結構創建對應的 struct 模型。久而久之,這是一個非常繁瑣的過程。接下來便使用 xorm根據數據庫表結構進行model 的 code 生成!

go get github.com/go-xorm/cmd/xorm
go get github.com/go-xorm/xorm

cd GOPATH/src/github.com/go-xorm/cmd/xorm

exec:

go build

根據 golang 版本不同有可能提示

go: inconsistent vendoring in /Users/zhuxiaojie/go/src/github.com/go-xorm/cmd/xorm:
run 'go mod vendor' to sync, or use -mod=mod or -mod=readonly to ignore the vendor directory

直接運行 go mod vendor 即可解決

接下來執行:
./xorm reverse mysql root:password@test?charset=utf8 templates/goxorm

有的同學可能會遇到

reverse.go:196 Error 1045: Access denied for user '“root'@'172.17.0.1' (using password: YES)

因為mysql 運行在 docker 中,查看源碼後其實只需要做出一下修改即可

./xorm reverse mysql "root:password@tcp(localhost:port)/db_name" templates/goxorm

接下來在當前目錄models中生成數據庫 model 文件。

Leave a Reply

Your email address will not be published. Required fields are marked *