Giter VIP home page Giter VIP logo

Comments (1)

tranvantri avatar tranvantri commented on September 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.