大道至简,简即是繁
- 个人网站: https://colobu.com
- 最好的Go微服务框架: https://rpcx.io
- rpcx官方博客: https://blog.rpcx.io
Converts a database into gorm structs and RESTful api
License: Apache License 2.0
大道至简,简即是繁
In my legacy system, there are one or two tables which I do not want to generate any codes for them. How to do?
'officially' being available for sqlx default would be great
Hi,
i tried to get structs of an existing mssql database, but it fails with an error
gen --sqltype "sqlserver" --connstr "server=localhost;user id=sa;password={xxx}" --database xxx --json --gorm
=> 2019/05/24 14:09:39 unknown db driver *mssql.Driver
same with
gen --sqltype "mssql" --connstr "server=localhost;user id=sa;password={xxx}" --database xxx --json --gorm
different with "asdasd" instead of "mssql". Is this a bug, or do i use it wrong?
if table name is 'order', the struct generated wiill be empty:
package model
import (
"database/sql"
"time"
"github.com/guregu/null"
)
var (
_ = time.Second
_ = sql.LevelDefault
_ = null.Bool{}
)
type Order struct {
}
// TableName sets the insert table name for this struct type
func (o *Order) TableName() string {
return "order"
}
gen param isgen --connstr 'xxxx' --table order --gorm --json
when I change table to order2, bug not show again
create table companies
(
id uuid default public.uuid_generate_v4() not null constraint clients_pkey
tags character varying[] default '{}'::character varying[],
deleted_by_id uuid,
created_by_id uuid,
);
gen
outputs errors like:table: companies unable to generate struct field: id type: UUID error: unknown sql type: uuid
table: companies unable to generate struct field: tags type: _VARCHAR error: unknown sql type: _varchar
table: companies unable to generate struct field: deleted_by_id type: UUID error: unknown sql type: uuid
table: companies unable to generate struct field: created_by_id type: UUID error: unknown sql type: uuid
gen
doesn't support the uuid
typegen
doesn't support the CHARACTER Varying[]
typeI clone this code and go mod tidy ,show this error,generated/model: malformed module path "generated/model": missing dot in first path element.
github.com/jimsmart/schema
I am having a hard time seeing how other databases than mysql are supported for inspection. I cannot get sqlite database to work with the command line tool, and the code has mysql driver hardcoded.
The generated code doesn't compile, it has an extra unused import. It imports by default gorm
in every model.
Lines 13 to 17 in 34eddd9
In MS SQL , I have some columns of data type datetime2
which generates errors like DATETIME2 error: unknown sql type: datetime2
.
I suppose it can be fixed in the mapping.json or is there any option to use it ?
All generated models have no properties generated
$ go version
go version go1.12.7 darwin/amd64
gen --connstr "root:root@tcp(127.0.0.1:3306)/abc?&parseTime=True" --database abc --json --gorm --guregu --rest
package model
import (
"database/sql"
"time"
"github.com/guregu/null"
)
var (
_ = time.Second
_ = sql.LevelDefault
_ = null.Bool{}
)
type Transaction struct {
}
// TableName sets the insert table name for this struct type
func (t *Transaction) TableName() string {
return "transaction"
}
`package model
import (
"database/sql"
"time"
"github.com/guregu/null"
)
var (
_ = time.Second
_ = sql.LevelDefault
_ = null.Bool{}
)
type UserAliinfo struct {
}
// TableName sets the insert table name for this struct type
func (u *UserAliinfo) TableName() string {
return "user_aliinfo"
}
`
Hello,
I wanted to create structs from a mysql database for gorm (no other features) like this - from a db hosted on localhost:
gen --sqltype=mysql \
--connstr "$DB_USER:$DB_PASS@/$DB_NAME" \
--database $DB_NAME \
--json \
--out ./models \
--gorm \
--overwrite
what I am getting is this:
Error loading template model_base.go.tmpl not found
am I missing something?
If I copy the file from the gen
repo into a local folder and pass the templateDir parameter, I do get results, but all accompanied by this error:
Error in loading model template, error: template: model:20: function "IsNullable" not defined
in dbmeta/meta.go GenerateStruct function line101, when the tablename is user , the sql string is SELECT * FROM user
LIMIT 0 . that is a error sql query string.
Does this package support gorm optional structs tags such as Size, PRIMARY_KEY, AUTO_INCREMENT
?
https://gorm.io/docs/models.html#Supported-Struct-tags
type Batch struct {
ID int `gorm:"column:id;primary_key" json:"id"`
Title string `gorm:"column:title" json:"title"`
Description sql.NullString `gorm:"column:description" json:"description"`
UrlsCount int `gorm:"column:urls_count" json:"urls_count"`
CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
}
above struct was created with following command but no optional struct tags
gen --connstr "USER:PASSWORD@tcp(127.0.0.1:3306)/dbname" --database dbname --table batches --gorm --json
such as fasthttprouter, gorilla/mux
gen --sqltype=mysql
generates dao with:
SELECT * FROM `account_members` WHERE member_id = $1
I believe it should generate dao with:
SELECT * FROM `account_members` WHERE member_id = ?
as title , gen don't support json type
below does NOT generate sqlx tags in models, but DOES generate protobuf tags
gen -v \
--sqltype=mysql \
--connstr "`cat db_conn_file.txt`" \
--json \
--out server \
--generate-dao \
--overwrite \
--database trictrac \
--model=models \
--module=trictr.ac/server \
--templateDir=template-gen \
--db \
--guregu
ex:
type AccountMember struct {
//[ 0] member_id varchar(36) null: false primary: true auto: false col: varchar len: 36 default: []
MemberID string `json:"member_id" protobuf:"string,0,opt,name=member_id"`
//[ 1] email_address text(65535) null: true primary: false auto: false col: text len: 65535 default: [NULL]
EmailAddress null.String `json:"email_address" protobuf:"string,1,opt,name=email_address"`
//[ 2] first_name text(65535) null: true primary: false auto: false col: text len: 65535 default: [NULL]
FirstName null.String `json:"first_name" protobuf:"string,2,opt,name=first_name"`
//[ 3] last_name text(65535) null: true primary: false auto: false col: text len: 65535 default: [NULL]
LastName null.String `json:"last_name" protobuf:"string,3,opt,name=last_name"`
//[ 4] joined_timestamp timestamp null: false primary: false auto: false col: timestamp len: -1 default: [current_timestamp()]
JoinedTimestamp time.Time `json:"joined_timestamp" protobuf:"uint64,4,opt,name=joined_timestamp"`
//[ 5] role text(65535) null: false primary: false auto: false col: text len: 65535 default: []
Role string `json:"role" protobuf:"string,5,opt,name=role"`
//[ 6] hashauth text(65535) null: false primary: false auto: false col: text len: 65535 default: []
Hashauth string `json:"hashauth" protobuf:"string,6,opt,name=hashauth"`
}
below DOES generates sqlx tags in models (as well as protobuf)
gen -v \
--sqltype=mysql \
--connstr "`cat db_conn_file.txt`" \
--json \
--out server \
--generate-dao \
--overwrite \
--database trictrac \
--model=models \
--module=trictr.ac/server \
--templateDir=template-gen \
--db \
--protobuf \
--guregu
ex:
type AccountMember struct {
//[ 0] member_id varchar(36) null: false primary: true auto: false col: varchar len: 36 default: []
MemberID string `json:"member_id" db:"member_id" protobuf:"string,0,opt,name=member_id"`
//[ 1] email_address text(65535) null: true primary: false auto: false col: text len: 65535 default: [NULL]
EmailAddress null.String `json:"email_address" db:"email_address" protobuf:"string,1,opt,name=email_address"`
//[ 2] first_name text(65535) null: true primary: false auto: false col: text len: 65535 default: [NULL]
FirstName null.String `json:"first_name" db:"first_name" protobuf:"string,2,opt,name=first_name"`
//[ 3] last_name text(65535) null: true primary: false auto: false col: text len: 65535 default: [NULL]
LastName null.String `json:"last_name" db:"last_name" protobuf:"string,3,opt,name=last_name"`
//[ 4] joined_timestamp timestamp null: false primary: false auto: false col: timestamp len: -1 default: [current_timestamp()]
JoinedTimestamp time.Time `json:"joined_timestamp" db:"joined_timestamp" protobuf:"uint64,4,opt,name=joined_timestamp"`
//[ 5] role text(65535) null: false primary: false auto: false col: text len: 65535 default: []
Role string `json:"role" db:"role" protobuf:"string,5,opt,name=role"`
//[ 6] hashauth text(65535) null: false primary: false auto: false col: text len: 65535 default: []
Hashauth string `json:"hashauth" db:"hashauth" protobuf:"string,6,opt,name=hashauth"`
}
this is at commit -> 0c7236f
Hi,
I have struct with null.String data type (generated through smallnest/gen), however I got the error
./user_related_unit_test.go:92:82: cannot use "abc" (type string) as type null.String in field value
when I tried to initialize it:
UserCredentialObject := model.UserCredential{UserID: 1, Sequence: 1, AesPwdOld: "abc", AesPwdNew: null, PinBlockOld: "1234", PinBlockNew: "5678"}
gen
version0.9.7 (06/09/2020)
macOS 10.14.6
zsh 5.3 (x86_64-apple-darwin18.0)
~ ❯❯❯ brew info postgresql
postgresql: stable 12.3 (bottled), HEAD
Run gen --sqltype=postgres --connstr=...
, it works.
Run make regen
with default connstr
param,
--connstr=host=127.0.0.1 \
port=5432 \
user=myname \
password=mypass \
dbname=mydb \
sslmode=disable \
it occurred with
Error pinging database: pq: SSL is not enabled on the server
Error in initializing db pq: SSL is not enabled on the server
make: *** [regen] Error 1
--connstr
in Makefile to below and run make regen
, it should works well. --connstr="host=127.0.0.1 port=5432 user=myname password=mypass dbname=mydb sslmode=disable connect_timeout=10000" \
But --connstr
in Makefile had been changed to default format like step 2.
I'm using zsh with zprezto.
Maybe it only broken in zsh? I don't know.
As title. Is it any way to generate json tag using camel case? Thanks!
gen --version
0.9.7 (06/09/2020)
gen --sqltype=postgresql --connstr=... --exclude=table_should_ignored
--exclude
to regen
part in Makefile
.make regen
cat Makefile
The --exclude
param not generated in the regen
part.
Hi,
I see you are using my 'schema' package! This is just a quick note to inform you of its limitations...
It works very well with some drivers, and not so well with others: sadly, not all drivers provide full support for methods on sql.ColumnType
Drivers with good/working .Nullable()
support:
But I have not yet found a driver with support for .Nullable()
on Postgres nor SQLite.
For further details see the report(s) at https://github.com/jimsmart/drivercaps — it's quite a can of worms.
I think the best solution is for the upstream drivers to add this missing support? But I have not yet filed any issues upstream, nor had time to look at any of the drivers' code.
HTH
I can't connect to a Postgres db, line 67 of main.go has static code for connection type.
var db, err = sql.Open("mysql", *sqlConnStr)
Suggest adding an execution parameter for specifying database type.
Getting this error when using tables (e.g. "mytable") that uses the timestamptz type in postgresql database:-
table: mytable unable to generate struct field: created type: TIMESTAMPTZ error: unknown sql type: timestamptz
gorm库虽然stars很高,但个人觉得使用起来没有xorm方便,也没sqlx性能好,希望能支持xorm库
sql 语句
CREATE TABLE `user` (
`id` bigint(0) NOT NULL AUTO_INCREMENT COMMENT '主键id',
`user_id` int(0) UNSIGNED NOT NULL COMMENT '用户id',
`role` int(0) NOT NULL DEFAULT 0 COMMENT '在此平台角色0用户1代购2个体商户3推广员4总代',
`vip_level` int(0) NULL DEFAULT NULL COMMENT 'vip等级',
`union_id` varchar(45) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '微信唯一索引',
`open_id` varchar(45) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '微信openid',
`nick_name` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '微信昵称',
`avatar_url` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '微信头像',
`gender` tinyint(0) NOT NULL COMMENT '性别,0男生1女生',
`country` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '所在国家',
`province` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '省份',
`city` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '城市',
`language` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '语种',
`bind_user_id` int(0) NULL DEFAULT NULL COMMENT '绑定人员',
`phone_number` bigint(0) NULL DEFAULT NULL COMMENT '手机号码',
`due_time` datetime(0) NULL DEFAULT NULL COMMENT '到期时间',
`create_time` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
`update_time` datetime(0) NULL DEFAULT CURRENT_TIMESTAMP(0) COMMENT '修改时间',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE INDEX `openid_UNIQUE`(`open_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = DYNAMIC;
生成的model
type User struct {
ID int64 `gorm:"AUTO_INCREMENT;column:id;type:BIGINT;primary_key" json:"id"` //[ 0] id bigint null: false primary: true auto: true col: bigint len: -1 default: []
UserID int `gorm:"column:user_id;type:INT;" json:"user_id"` //[ 1] user_id int null: false primary: false auto: false col: int len: -1 default: []
Role int `gorm:"column:role;type:INT;default:0;" json:"role"` //[ 2] role int null: false primary: false auto: false col: int len: -1 default: [0]
VipLevel null.Int `gorm:"column:vip_level;type:INT;" json:"vip_level"` //[ 3] vip_level int null: true primary: false auto: false col: int len: -1 default: []
UnionID string `gorm:"column:union_id;type:VARCHAR;size:45;" json:"union_id"` //[ 4] union_id varchar(45) null: false primary: false auto: false col: varchar len: 45 default: []
OpenID string `gorm:"column:open_id;type:VARCHAR;size:45;" json:"open_id"` //[ 5] open_id varchar(45) null: false primary: false auto: false col: varchar len: 45 default: []
NickName null.String `gorm:"column:nick_name;type:VARCHAR;size:100;" json:"nick_name"` //[ 6] nick_name varchar(100) null: true primary: false auto: false col: varchar len: 100 default: []
AvatarURL string `gorm:"column:avatar_url;type:VARCHAR;size:255;" json:"avatar_url"` //[ 7] avatar_url varchar(255) null: false primary: false auto: false col: varchar len: 255 default: []
Gender int `gorm:"column:gender;type:TINYINT;" json:"gender"` //[ 8] gender tinyint null: false primary: false auto: false col: tinyint len: -1 default: []
Country null.String `gorm:"column:country;type:VARCHAR;size:100;" json:"country"` //[ 9] country varchar(100) null: true primary: false auto: false col: varchar len: 100 default: []
Province null.String `gorm:"column:province;type:VARCHAR;size:100;" json:"province"` //[10] province varchar(100) null: true primary: false auto: false col: varchar len: 100 default: []
City null.String `gorm:"column:city;type:VARCHAR;size:100;" json:"city"` //[11] city varchar(100) null: true primary: false auto: false col: varchar len: 100 default: []
Language null.String `gorm:"column:language;type:VARCHAR;size:100;" json:"language"` //[12] language varchar(100) null: true primary: false auto: false col: varchar len: 100 default: []
BindUserID null.Int `gorm:"column:bind_user_id;type:INT;" json:"bind_user_id"` //[13] bind_user_id int null: true primary: false auto: false col: int len: -1 default: []
PhoneNumber null.Int `gorm:"column:phone_number;type:BIGINT;" json:"phone_number"` //[14] phone_number bigint null: true primary: false auto: false col: bigint len: -1 default: []
DueTime null.Time `gorm:"column:due_time;type:DATETIME;" json:"due_time"` //[15] due_time datetime null: true primary: false auto: false col: datetime len: -1 default: []
CreateTime null.Time `gorm:"column:create_time;type:DATETIME;" json:"create_time"` //[16] create_time datetime null: true primary: false auto: false col: datetime len: -1 default: []
UpdateTime null.Time `gorm:"column:update_time;type:DATETIME;default:CURRENT_TIMESTAMP;" json:"update_time"` //[17] update_time datetime null: true primary: false auto: false col: datetime len: -1 default: [CURRENT_TIMESTAMP]
}
可以看到 从mysql生成golang结构体时,不支持unsigned int, mysql定义的字段类型是unsigned int,但是generated
出来的结构体是int, 如果使用mapping.json配置的话,估计也不能解决.
Whether to consider generate comments for each field in the database after the field as well when generate model code
gen --sqltype=postgres --connstr "host=127.0.0.1 port=5432 user=$PGUSER dbname=$PGDATABASE password=$PGPASSWORD sslmode=disable connect_timeout=10000" --database $PGDATABASE --json --gorm --guregu --rest
The point type is not supported currently. Perhaps it must be supported. Other geometric datatypes are probably not supported either.
Now it generated invalid go-code. It generates a record in the structure without a type. When the type is unknown, it is perhaps better to omit the column.
See https://dev.mysql.com/doc/refman/5.7/en/spatial-type-overview.html
表创建语句
DROP TABLE IF EXISTS `test_db`;
CREATE TABLE `test_db` (
`id` int(0) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键',
`created_at` datetime(0) NOT NULL COMMENT '创建时间',
`updated_at` datetime(0) NOT NULL COMMENT '更新时间',
`deleted_at` datetime(0) NULL DEFAULT NULL COMMENT '删除时间',
`type_id` bigint(0) UNSIGNED NOT NULL DEFAULT 0,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`age` int(0) NULL DEFAULT NULL,
`avatar_url` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`passwd` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`created` datetime(0) NULL DEFAULT NULL,
`updated` datetime(0) NULL DEFAULT NULL,
`gender` enum('one','two') CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL,
`birthday` datetime(0) NULL DEFAULT NULL,
`email` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`detial` json NULL,
`price` decimal(10, 2) UNSIGNED NULL DEFAULT NULL,
PRIMARY KEY (`id`, `type_id`) USING BTREE,
UNIQUE INDEX `uix_test_db_email`(`email`) USING BTREE,
INDEX `idx_test_db_deleted_at`(`deleted_at`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 24 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = DYNAMIC;
SET FOREIGN_KEY_CHECKS = 1;
其中price,生成model时会报错 table: test_db unable to generate struct field: price type: udecimal error: unknown sql type: udecimal
.
page, err := readInt(r, "page", 1)
27
where is readint function?
All my tables had same prefix like xxx_
, for example:
xxx_users
xxx_posts
xxx_tokens
The generated codes like model
, dao
, routes all have xxx
included, like XxxUser
, GetAllXxxUser
and /xxxuser
, /xxxuser/:argId
.
I wanna request a new feature like adding something like a param named prefix
and auto stripping the prefix when generating codes. The ideal model
, dao
, routes look likes following User
, GetAllUser
, /user
and /user/:argId
default dao 'getall' with 'paging' writes the sql and ALSO passes as parameters
this causes:
sql: expected 0 arguments, got 2
errors
err = DB.SelectContext(ctx, &structs, sql, page, pagesize)
should not pass page and pagesize, as the fmt.Sprintf is actually embedding the parameters in the actual query string
gen always import github.com/guregu/null
and will generate
....
var (
_ = null.Bool()
)
gen --connstr "dev/123@tcp(192.168.1.242:3306)/gyys_user_center?charset=utf8mb4&parseTime=True&loc=Local" --database doctor_auth --json --gorm --guregu --rest
MySQL 8.0
gen -c 'mitra:shopee123!@tcp(10.12.77.233)/apc_report_db?&parseTime=True' --database apc_report_db -t dp_order_report_tab --package dbreport --json --gorm --guregu
package model
import (
"database/sql"
"time"
"github.com/guregu/null"
)
var (
_ = time.Second
_ = sql.LevelDefault
_ = null.Bool{}
)
type DpOrderReportTab struct {
}
// TableName sets the insert table name for this struct type
func (d *DpOrderReportTab) TableName() string {
return "dp_order_report_tab"
}
Run as $ gen -v --connstr "root:roooooot@/test" --package test --database test --table label --json --gorm
Nothing hanppend.
ENV:
mysql Ver 8.0.18 for macos10.14 on x86_64
go version go1.13.1 darwin/amd64
table create statement
DROP TABLE IF EXISTS `express_company`;
CREATE TABLE `express_company` (
`id` int(0) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键',
`express_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL COMMENT '快递公司名称',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 7 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = DYNAMIC;
version:0.9.12
when I use this table to create model,generate code is like:
// ExpressCompany struct is a row record of the express_company table in the daigou database
type ExpressCompany struct {
//[ 0] id uint null: false primary: true isArray: false auto: true col: uint len: -1 default: []
ID uint32 `gorm:"primary_key;AUTO_INCREMENT;column:id;type:uint;" json:"id"`
//[ 1] express_name varchar null: false primary: false isArray: false auto: false col: varchar len: 0 default: []
ExpressName string `gorm:"column:express_name;type:varchar;" json:"express_name"`
}
I am reviewing the command line options but do not understand how to connect to a sqlite3 database. I have a file.db
and the table name is account
. I want to create output suitable for a gorm
model.
What command line options should I use?
如果mysql里面定义了一张表,最后一个字母是s,生成的golang文件会去掉s,生成的model结构体也会去掉s.
mustexec causes panic in key violations ... when normally you would want to catch those errors yourself.
an option to not use mustexec by default in dao's would be stellar.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.