Use model's TableName() in Cols for dynamic table names

This commit is contained in:
2026-03-29 15:43:42 +02:00
parent 97e6f82737
commit f6506584d1
5 changed files with 36 additions and 40 deletions

View File

@@ -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
}