{"record":{"id":"d8f8961ea085c933","repo":"vxcontrol/pentagi","slug":"user-id-is-required","errorCode":null,"errorMessage":"user_id is required","messagePattern":"user_id is required","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/pkg/server/models/users.go","lineNumber":278,"sourceCode":"\n// UserPreferences is model to contain user preferences information\ntype UserPreferences struct {\n\tID          uint64                 `json:\"id\" gorm:\"type:BIGINT;NOT NULL;PRIMARY_KEY;AUTO_INCREMENT\"`\n\tUserID      uint64                 `json:\"user_id\" gorm:\"type:BIGINT;NOT NULL;UNIQUE_INDEX\"`\n\tPreferences UserPreferencesOptions `json:\"preferences\" gorm:\"type:JSONB;NOT NULL\"`\n\tCreatedAt   time.Time              `json:\"created_at\" gorm:\"type:TIMESTAMPTZ;NOT NULL;default:CURRENT_TIMESTAMP\"`\n\tUpdatedAt   time.Time              `json:\"updated_at\" gorm:\"type:TIMESTAMPTZ;NOT NULL;default:CURRENT_TIMESTAMP\"`\n}\n\n// TableName returns the table name string to guaranty use correct table\nfunc (up *UserPreferences) TableName() string {\n\treturn \"user_preferences\"\n}\n\n// Valid is function to control input/output data\nfunc (up UserPreferences) Valid() error {\n\tif up.UserID == 0 {\n\t\treturn fmt.Errorf(\"user_id is required\")\n\t}\n\treturn nil\n}\n\n// Validate is function to use callback to control input/output data\nfunc (up UserPreferences) Validate(db *gorm.DB) {\n\tif err := up.Valid(); err != nil {\n\t\tdb.AddError(err)\n\t}\n}\n\n// NewUserPreferences creates a new UserPreferences with default values\nfunc NewUserPreferences(userID uint64) *UserPreferences {\n\treturn &UserPreferences{\n\t\tUserID: userID,\n\t\tPreferences: UserPreferencesOptions{\n\t\t\tFavoriteFlows: []int64{},\n\t\t},","sourceCodeStart":260,"sourceCodeEnd":296,"githubUrl":"https://github.com/vxcontrol/pentagi/blob/ea665308baaff015b226f308438a68d929d0f29b/backend/pkg/server/models/users.go#L260-L296","documentation":"UserPreferences.Valid() (backend/pkg/server/models/users.go:278) requires the UserID foreign key to be non-zero before the preferences row is considered valid; otherwise it returns this error. It runs from the Validate GORM callback on create/update, so an orphaned UserPreferences row (no owning user) is never written.","triggerScenarios":"Saving a UserPreferences struct with the zero value for UserID, e.g. building the struct without setting UserID or copying only preference fields from another object.","commonSituations":"Creating preferences for a user that has not been created yet (no ID assigned), forgetting to pass the user ID through a handler, or mass-assigning request JSON into the model where user_id was never supplied.","solutions":["Set up.UserID (the already-persisted user's ID) before creating or updating the preferences record.","Create the User first and use its returned primary key when constructing UserPreferences.","If preferences should always belong to a user, create them via an association (e.g. orm.Association) so UserID is populated automatically."],"exampleFix":"// before\npref := models.UserPreferences{Language: \"en\"}\norm.Create(&pref) // user_id is required\n\n// after\npref := models.UserPreferences{UserID: user.ID, Language: \"en\"}\norm.Create(&pref)","handlingStrategy":"validation","validationCode":"if pref.UserID == 0 {\n    return fmt.Errorf(\"cannot save preferences without a user\")\n}","typeGuard":"func hasOwner(pref models.UserPreferences) bool {\n    return pref.UserID != 0\n}","tryCatchPattern":"if err := orm.Create(&pref).Error; err != nil {\n    if err.Error() == \"user_id is required\" {\n        return fmt.Errorf(\"preferences require a persisted user (set UserID)\")\n    }\n    return err\n}","preventionTips":["Always create the User first and copy its generated ID into UserID.","Build preferences through the user association rather than standalone structs.","Never bind user_id directly from untrusted request input without checking non-zero."],"tags":["go","validation","gorm","foreign-key"],"backgroundTag":"missing-required-argument","analyzedSha":"ea665308baaff015b226f308438a68d929d0f29b","analyzedAt":"2026-09-01T14:16:31.421Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}