Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@ademozay
Copy link
Contributor

@ademozay ademozay commented Aug 16, 2018

Instead of tagging every single struct field, this PR is useful when you want to add prefix/suffix or alter the database/table/column names.
Default naming strategy(snake case) is being applied without any configuration but if you like to set custom naming strategy you can use below

func main() {
	db, err := gorm.Open("sqlite3", "test.db")
	if err != nil {
		panic("failed to connect database")
	}
	defer db.Close()

	gorm.AddNamingStrategy(&gorm.NamingStrategy{
		DB: func(name string) string {
			return "db_" + name
		},
		Table: func(name string) string {
			return "table_" + name
		},
		Column: func(name string) string {
			return "col_" + name
		},
	})

	db.AutoMigrate(&Product{})
}

@pedromorgan
Copy link
Collaborator

This look great, and what stopped my using GORM on a recent project

@jinzhu jinzhu merged commit 6f58f8a into go-gorm:master Sep 9, 2018
@jinzhu
Copy link
Member

jinzhu commented Sep 9, 2018

Looks great, thank you.

@fieryorc
Copy link

Thanks!

@theoneLee theoneLee mentioned this pull request Oct 15, 2019
blefevre pushed a commit to blefevre/gorm that referenced this pull request Feb 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants