Comments (1)
You can try:
gormDb, err = gorm.Open(postgres.Open(dsn), &gorm.Config{
NamingStrategy: schema.NamingStrategy{
IdentifierMaxLength: 64, // Default Identifier length is 64
SingularTable: true,
},
})
The SingularTable config is used for the table name and field name. When SingularTable equals:
- True: table name and field name are plural form
- False: table name and field name are singular form
For me, I want to keep the table name in singular form. Field names like database
gormDb, err := gorm.Open(postgres.Open(dsn))
if err != nil {
panic(err)
}
tableList, err := gormDb.Migrator().GetTables()
if err != nil {
panic(fmt.Errorf("get all tables fail: %w", err))
}
modelNames := map[string]string{}
for _, tableName := range tableList {
modelNames[tableName] = gormDb.NamingStrategy.SchemaName(tableName)
}
db, err := gormDb.DB()
if err != nil {
panic(err)
}
err = db.Close()
if err != nil {
panic(err)
}
gormDb, err = gorm.Open(postgres.Open(dsn), &gorm.Config{
NamingStrategy: schema.NamingStrategy{
IdentifierMaxLength: 64, // Default Identifier length is 64
SingularTable: true,
},
})
g.UseDB(gormDb)
tables := make([]interface{}, len(tableList))
for i, tableName := range tableList {
tables[i] = g.GenerateModelAs(tableName, modelNames[tableName])
}
// generate target setting
g.ApplyBasic(tables...)
// Generate the code
from gen.
Related Issues (20)
- embed custom model for generated model
- Is there a way to add `db` tag HOT 1
- How to keep model name 's' prefix? HOT 1
- gorm Gen Tool int32
- Same table names cannot be generated across different schemes HOT 2
- left join 时候对关联表软删除数据没做where过滤 HOT 2
- 如何不重复生成query/gen.go HOT 1
- 使用joins+select,gen与gorm执行的sql效果不一致
- How to sort on a foreign key constraint's column count? HOT 1
- @@Table: Annotations are not dynamic
- 当在数据库中申明default为NULL时,通过gen生成的代码好像不会加上这个tag
- expected 'IDENT', found '[' (and 1 more errors)
- field.Field 缺失 NotIn 方法
- If there is a unique index in the generated unit test, an error will be reported. HOT 2
- How to customize the field names of the generated model? HOT 1
- 求问, 注解可以支持返回自定义对象吗? HOT 2
- Why smallint is mapped to int32 by default? HOT 1
- 调用 UpdateSimple 更新数据时, 回调函数不能正常工作 HOT 1
- insufficient arguments 我在使用 PostgreSQL 时遇到的情况,`g.ApplyBasic(g.GenerateAllTable()...,)` 报错: HOT 1
- 指定表名后生成的sql无法执行 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gen.