How to get a table name from a model in gorm?

Issue

Is it possible to get model’s table name? I see that it’s possible to get it from ModelStruct but I don’t know how to do it correctly. I didn’t find any initializations for this structure.

user := User{}
tableName := db...

Solution

Like this:

tableName := db.NewScope(model).GetModelStruct().TableName(db)

Update:
Shorter

tableName := db.NewScope(model).TableName() 

Answered By – fr05t1k

Answer Checked By – Dawn Plyler (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.