Schema
The Schema interface is the foundation of all models in forge. It defines how your data is structured and how it behaves.
Interface​
All models must implement the Schema interface:
type Schema interface {
Fields() []Field
Relations() []Relation
Meta() Meta
Hooks() *ModelHooks
}
BaseSchema​
Embed BaseSchema in your models:
type User struct {
schema.BaseSchema
}
Fields() Method​
Returns all field definitions for the model:
func (User) Fields() []schema.Field {
return []schema.Field{
schema.Int64("id").Primary().AutoIncrement().Build(),
schema.String("username").Required().MaxLength(150).Build(),
}
}
Relations() Method​
Returns relationship definitions:
func (User) Relations() []schema.Relation {
return []schema.Relation{
relations.ForeignKey("profile", "UserProfile").
OnDelete(schema.Cascade),
}
}
Meta() Method​
Returns model metadata:
func (User) Meta() schema.Meta {
return schema.Meta{
TableName: "users",
VerboseName: "User",
VerboseNamePlural: "Users",
OrderBy: []string{"-date_joined"},
}
}
Hooks() Method​
Returns lifecycle hooks:
func (User) Hooks() *schema.ModelHooks {
return &schema.ModelHooks{
BeforeCreate: func(ctx context.Context, instance interface{}) error {
// Hook logic
return nil
},
}
}
Meta Options​
TableName​
Custom table name:
TableName: "custom_users"
VerboseName​
Human-readable singular name:
VerboseName: "User"
VerboseNamePlural​
Human-readable plural name:
VerboseNamePlural: "Users"
OrderBy​
Default ordering:
OrderBy: []string{"-date_joined", "username"}
Use - prefix for descending order.
Indexes​
Custom indexes:
Indexes: []schema.Index{
{
Name: "idx_user_email",
Fields: []string{"email"},
Unique: false,
},
}
UniqueTogether​
Composite unique constraints:
UniqueTogether: [][]string{
{"username", "email"},
}
Constraints​
Check constraints:
Constraints: []schema.UniqueConstraint{
{
Name: "check_age",
Check: "age >= 0",
},
}
ModelHooks​
Lifecycle hooks:
type ModelHooks struct {
BeforeCreate func(ctx context.Context, instance interface{}) error
AfterCreate func(ctx context.Context, instance interface{}) error
BeforeUpdate func(ctx context.Context, instance interface{}) error
AfterUpdate func(ctx context.Context, instance interface{}) error
BeforeSave func(ctx context.Context, instance interface{}) error
AfterSave func(ctx context.Context, instance interface{}) error
BeforeDelete func(ctx context.Context, instance interface{}) error
AfterDelete func(ctx context.Context, instance interface{}) error
Clean func(ctx context.Context, instance interface{}) error
Validate func(ctx context.Context, instance interface{}) error
}
See Also​
- Fields Reference - Complete field type reference
- Relations Reference - Relationship types
- Hooks Reference - Lifecycle hooks