vxcontrol/pentagi · error

user_id is required

Error message

user_id is required

What it means

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.

Source

Thrown at backend/pkg/server/models/users.go:278

// UserPreferences is model to contain user preferences information
type UserPreferences struct {
	ID          uint64                 `json:"id" gorm:"type:BIGINT;NOT NULL;PRIMARY_KEY;AUTO_INCREMENT"`
	UserID      uint64                 `json:"user_id" gorm:"type:BIGINT;NOT NULL;UNIQUE_INDEX"`
	Preferences UserPreferencesOptions `json:"preferences" gorm:"type:JSONB;NOT NULL"`
	CreatedAt   time.Time              `json:"created_at" gorm:"type:TIMESTAMPTZ;NOT NULL;default:CURRENT_TIMESTAMP"`
	UpdatedAt   time.Time              `json:"updated_at" gorm:"type:TIMESTAMPTZ;NOT NULL;default:CURRENT_TIMESTAMP"`
}

// TableName returns the table name string to guaranty use correct table
func (up *UserPreferences) TableName() string {
	return "user_preferences"
}

// Valid is function to control input/output data
func (up UserPreferences) Valid() error {
	if up.UserID == 0 {
		return fmt.Errorf("user_id is required")
	}
	return nil
}

// Validate is function to use callback to control input/output data
func (up UserPreferences) Validate(db *gorm.DB) {
	if err := up.Valid(); err != nil {
		db.AddError(err)
	}
}

// NewUserPreferences creates a new UserPreferences with default values
func NewUserPreferences(userID uint64) *UserPreferences {
	return &UserPreferences{
		UserID: userID,
		Preferences: UserPreferencesOptions{
			FavoriteFlows: []int64{},
		},

View on GitHub (pinned to ea665308ba)

Solutions

  1. Set up.UserID (the already-persisted user's ID) before creating or updating the preferences record.
  2. Create the User first and use its returned primary key when constructing UserPreferences.
  3. If preferences should always belong to a user, create them via an association (e.g. orm.Association) so UserID is populated automatically.

Example fix

// before
pref := models.UserPreferences{Language: "en"}
orm.Create(&pref) // user_id is required

// after
pref := models.UserPreferences{UserID: user.ID, Language: "en"}
orm.Create(&pref)
Defensive patterns

Strategy: validation

Validate before calling

if pref.UserID == 0 {
    return fmt.Errorf("cannot save preferences without a user")
}

Type guard

func hasOwner(pref models.UserPreferences) bool {
    return pref.UserID != 0
}

Try / catch

if err := orm.Create(&pref).Error; err != nil {
    if err.Error() == "user_id is required" {
        return fmt.Errorf("preferences require a persisted user (set UserID)")
    }
    return err
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of vxcontrol/pentagi@ea665308ba (2026-09-01). Data as JSON: /api/errors/d8f8961ea085c933. Report an issue: GitHub.