Use model's TableName() in Cols for dynamic table names
This commit is contained in:
15
gormcol.go
15
gormcol.go
@@ -9,26 +9,19 @@
|
||||
// IDProfile gormcol.Field
|
||||
// IDAuthorizationRole gormcol.Field
|
||||
// }{
|
||||
// IDProfile: gormcol.Field{Column: "id_profile"},
|
||||
// IDAuthorizationRole: gormcol.Field{Column: "id_authorization_role"},
|
||||
// IDProfile: gormcol.Field{Table: (PsAccess{}).TableName(), Column: "id_profile"},
|
||||
// IDAuthorizationRole: gormcol.Field{Table: (PsAccess{}).TableName(), Column: "id_authorization_role"},
|
||||
// }
|
||||
//
|
||||
// Then in queries, use the model's TableName():
|
||||
//
|
||||
// PsAccess{}.TableName() + "." + gormcol.Column(PsAccessCols.IDProfile) // "ps_access.id_profile"
|
||||
package gormcol
|
||||
|
||||
// Field represents a GORM column descriptor.
|
||||
// It holds only the column name; use the model's TableName() for table qualification.
|
||||
// Table should be set using the model's TableName() function for type safety.
|
||||
type Field struct {
|
||||
Table string // Database table name (use model's TableName())
|
||||
Column string // Database column name
|
||||
}
|
||||
|
||||
// Column returns the column name from a Field descriptor.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// gormcol.Column(dbmodel.PsAccessCols.IDAuthorizationRole) // "id_authorization_role"
|
||||
func Column(f Field) string {
|
||||
return f.Column
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user