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...
GORM uses the field with the name ID as the table’s primary key by default. GORM pluralizes struct name to snake_cases as table name, for struct User, its table name is users by convention You can change the default table name by implementing the Tabler interface, for example:
Pluralized Table Name GORM pluralizes struct name to snake_cases as table name, for struct User, its table name is users by convention
Retrieving a single object GORM provides First, Take, Last method to retrieve a single object from the database, it adds LIMIT 1 condition when querying the database, and it will return error ErrRecordNotFound if no record found.
For more complicated SQL queries. please also refer to Group Conditions in Advanced Query. Select allows you to specify the fields that you want to retrieve from database. Otherwise, GORM will select all fields by default. ... ... ... You can use Joins eager loading associations with a single SQL, for example:
For Gorm v2, according to https://github.com/go-gorm/gorm/issues/3603, you can do:
stmt := &gorm.Statement{DB: DB}
stmt.Parse(&ColumnStruct2{})
stmt.Schema.Table
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With