// Code generated by BobGen psql v0.42.5. DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.

package models

import (
	"context"
	"fmt"
	"io"
	"time"

	"github.com/Gleipnir-Technology/bob"
	"github.com/Gleipnir-Technology/bob/dialect/psql"
	"github.com/Gleipnir-Technology/bob/dialect/psql/dialect"
	"github.com/Gleipnir-Technology/bob/dialect/psql/dm"
	"github.com/Gleipnir-Technology/bob/dialect/psql/sm"
	"github.com/Gleipnir-Technology/bob/dialect/psql/um"
	"github.com/Gleipnir-Technology/bob/expr"
	"github.com/Gleipnir-Technology/bob/mods"
	"github.com/Gleipnir-Technology/bob/orm"
	"github.com/Gleipnir-Technology/bob/types/pgtypes"
	enums "github.com/Gleipnir-Technology/nidus-sync/db/enums"
	"github.com/aarondl/opt/null"
	"github.com/aarondl/opt/omit"
	"github.com/aarondl/opt/omitnull"
)

// User is an object representing the database table.
type User struct {
	ID                        int32                             `db:"id,pk" `
	ArcgisAccessToken         null.Val[string]                  `db:"arcgis_access_token" `
	ArcgisLicense             null.Val[enums.Arcgislicensetype] `db:"arcgis_license" `
	ArcgisRefreshToken        null.Val[string]                  `db:"arcgis_refresh_token" `
	ArcgisRefreshTokenExpires null.Val[time.Time]               `db:"arcgis_refresh_token_expires" `
	ArcgisRole                null.Val[string]                  `db:"arcgis_role" `
	DisplayName               string                            `db:"display_name" `
	Email                     null.Val[string]                  `db:"email" `
	OrganizationID            int32                             `db:"organization_id" `
	Username                  string                            `db:"username" `
	PasswordHashType          enums.Hashtype                    `db:"password_hash_type" `
	PasswordHash              string                            `db:"password_hash" `
	Role                      enums.Userrole                    `db:"role" `

	R userR `db:"-" `

	C userC `db:"-" `
}

// UserSlice is an alias for a slice of pointers to User.
// This should almost always be used instead of []*User.
type UserSlice []*User

// Users contains methods to work with the user_ table
var Users = psql.NewTablex[*User, UserSlice, *UserSetter]("", "user_", buildUserColumns("user_"))

// UsersQuery is a query on the user_ table
type UsersQuery = *psql.ViewQuery[*User, UserSlice]

// userR is where relationships are stored.
type userR struct {
	UserOauthTokens                 ArcgisOauthTokenSlice        // arcgis.oauth_token.oauth_token_user_id_fkey
	PublicUserUser                  ArcgisUserSlice              // arcgis.user_.user__public_user_id_fkey
	CreatorComplianceReportRequests ComplianceReportRequestSlice // compliance_report_request.compliance_report_request_creator_fkey
	CreatorFiles                    FileuploadFileSlice          // fileupload.file.file_creator_id_fkey
	FileuploadPool                  FileuploadPoolSlice          // fileupload.pool.pool_creator_id_fkey
	CreatorNoteAudios               NoteAudioSlice               // note_audio.note_audio_creator_id_fkey
	DeletorNoteAudios               NoteAudioSlice               // note_audio.note_audio_deletor_id_fkey
	CreatorNoteImages               NoteImageSlice               // note_image.note_image_creator_id_fkey
	DeletorNoteImages               NoteImageSlice               // note_image.note_image_deletor_id_fkey
	UserNotifications               NotificationSlice            // notification.notification_user_id_fkey
	CreatorPools                    PoolSlice                    // pool.pool_creator_id_fkey
	CreatorResidents                ResidentSlice                // resident.resident_creator_fkey
	CreatorSites                    SiteSlice                    // site.site_creator_id_fkey
	Organization                    *Organization                // user_.user__organization_id_fkey
}

func buildUserColumns(alias string) userColumns {
	return userColumns{
		ColumnsExpr: expr.NewColumnsExpr(
			"id", "arcgis_access_token", "arcgis_license", "arcgis_refresh_token", "arcgis_refresh_token_expires", "arcgis_role", "display_name", "email", "organization_id", "username", "password_hash_type", "password_hash", "role",
		).WithParent("user_"),
		tableAlias:                alias,
		ID:                        psql.Quote(alias, "id"),
		ArcgisAccessToken:         psql.Quote(alias, "arcgis_access_token"),
		ArcgisLicense:             psql.Quote(alias, "arcgis_license"),
		ArcgisRefreshToken:        psql.Quote(alias, "arcgis_refresh_token"),
		ArcgisRefreshTokenExpires: psql.Quote(alias, "arcgis_refresh_token_expires"),
		ArcgisRole:                psql.Quote(alias, "arcgis_role"),
		DisplayName:               psql.Quote(alias, "display_name"),
		Email:                     psql.Quote(alias, "email"),
		OrganizationID:            psql.Quote(alias, "organization_id"),
		Username:                  psql.Quote(alias, "username"),
		PasswordHashType:          psql.Quote(alias, "password_hash_type"),
		PasswordHash:              psql.Quote(alias, "password_hash"),
		Role:                      psql.Quote(alias, "role"),
	}
}

type userColumns struct {
	expr.ColumnsExpr
	tableAlias                string
	ID                        psql.Expression
	ArcgisAccessToken         psql.Expression
	ArcgisLicense             psql.Expression
	ArcgisRefreshToken        psql.Expression
	ArcgisRefreshTokenExpires psql.Expression
	ArcgisRole                psql.Expression
	DisplayName               psql.Expression
	Email                     psql.Expression
	OrganizationID            psql.Expression
	Username                  psql.Expression
	PasswordHashType          psql.Expression
	PasswordHash              psql.Expression
	Role                      psql.Expression
}

func (c userColumns) Alias() string {
	return c.tableAlias
}

func (userColumns) AliasedAs(alias string) userColumns {
	return buildUserColumns(alias)
}

// UserSetter is used for insert/upsert/update operations
// All values are optional, and do not have to be set
// Generated columns are not included
type UserSetter struct {
	ID                        omit.Val[int32]                       `db:"id,pk" `
	ArcgisAccessToken         omitnull.Val[string]                  `db:"arcgis_access_token" `
	ArcgisLicense             omitnull.Val[enums.Arcgislicensetype] `db:"arcgis_license" `
	ArcgisRefreshToken        omitnull.Val[string]                  `db:"arcgis_refresh_token" `
	ArcgisRefreshTokenExpires omitnull.Val[time.Time]               `db:"arcgis_refresh_token_expires" `
	ArcgisRole                omitnull.Val[string]                  `db:"arcgis_role" `
	DisplayName               omit.Val[string]                      `db:"display_name" `
	Email                     omitnull.Val[string]                  `db:"email" `
	OrganizationID            omit.Val[int32]                       `db:"organization_id" `
	Username                  omit.Val[string]                      `db:"username" `
	PasswordHashType          omit.Val[enums.Hashtype]              `db:"password_hash_type" `
	PasswordHash              omit.Val[string]                      `db:"password_hash" `
	Role                      omit.Val[enums.Userrole]              `db:"role" `
}

func (s UserSetter) SetColumns() []string {
	vals := make([]string, 0, 13)
	if s.ID.IsValue() {
		vals = append(vals, "id")
	}
	if !s.ArcgisAccessToken.IsUnset() {
		vals = append(vals, "arcgis_access_token")
	}
	if !s.ArcgisLicense.IsUnset() {
		vals = append(vals, "arcgis_license")
	}
	if !s.ArcgisRefreshToken.IsUnset() {
		vals = append(vals, "arcgis_refresh_token")
	}
	if !s.ArcgisRefreshTokenExpires.IsUnset() {
		vals = append(vals, "arcgis_refresh_token_expires")
	}
	if !s.ArcgisRole.IsUnset() {
		vals = append(vals, "arcgis_role")
	}
	if s.DisplayName.IsValue() {
		vals = append(vals, "display_name")
	}
	if !s.Email.IsUnset() {
		vals = append(vals, "email")
	}
	if s.OrganizationID.IsValue() {
		vals = append(vals, "organization_id")
	}
	if s.Username.IsValue() {
		vals = append(vals, "username")
	}
	if s.PasswordHashType.IsValue() {
		vals = append(vals, "password_hash_type")
	}
	if s.PasswordHash.IsValue() {
		vals = append(vals, "password_hash")
	}
	if s.Role.IsValue() {
		vals = append(vals, "role")
	}
	return vals
}

func (s UserSetter) Overwrite(t *User) {
	if s.ID.IsValue() {
		t.ID = s.ID.MustGet()
	}
	if !s.ArcgisAccessToken.IsUnset() {
		t.ArcgisAccessToken = s.ArcgisAccessToken.MustGetNull()
	}
	if !s.ArcgisLicense.IsUnset() {
		t.ArcgisLicense = s.ArcgisLicense.MustGetNull()
	}
	if !s.ArcgisRefreshToken.IsUnset() {
		t.ArcgisRefreshToken = s.ArcgisRefreshToken.MustGetNull()
	}
	if !s.ArcgisRefreshTokenExpires.IsUnset() {
		t.ArcgisRefreshTokenExpires = s.ArcgisRefreshTokenExpires.MustGetNull()
	}
	if !s.ArcgisRole.IsUnset() {
		t.ArcgisRole = s.ArcgisRole.MustGetNull()
	}
	if s.DisplayName.IsValue() {
		t.DisplayName = s.DisplayName.MustGet()
	}
	if !s.Email.IsUnset() {
		t.Email = s.Email.MustGetNull()
	}
	if s.OrganizationID.IsValue() {
		t.OrganizationID = s.OrganizationID.MustGet()
	}
	if s.Username.IsValue() {
		t.Username = s.Username.MustGet()
	}
	if s.PasswordHashType.IsValue() {
		t.PasswordHashType = s.PasswordHashType.MustGet()
	}
	if s.PasswordHash.IsValue() {
		t.PasswordHash = s.PasswordHash.MustGet()
	}
	if s.Role.IsValue() {
		t.Role = s.Role.MustGet()
	}
}

func (s *UserSetter) Apply(q *dialect.InsertQuery) {
	q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
		return Users.BeforeInsertHooks.RunHooks(ctx, exec, s)
	})

	q.AppendValues(bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
		vals := make([]bob.Expression, 13)
		if s.ID.IsValue() {
			vals[0] = psql.Arg(s.ID.MustGet())
		} else {
			vals[0] = psql.Raw("DEFAULT")
		}

		if !s.ArcgisAccessToken.IsUnset() {
			vals[1] = psql.Arg(s.ArcgisAccessToken.MustGetNull())
		} else {
			vals[1] = psql.Raw("DEFAULT")
		}

		if !s.ArcgisLicense.IsUnset() {
			vals[2] = psql.Arg(s.ArcgisLicense.MustGetNull())
		} else {
			vals[2] = psql.Raw("DEFAULT")
		}

		if !s.ArcgisRefreshToken.IsUnset() {
			vals[3] = psql.Arg(s.ArcgisRefreshToken.MustGetNull())
		} else {
			vals[3] = psql.Raw("DEFAULT")
		}

		if !s.ArcgisRefreshTokenExpires.IsUnset() {
			vals[4] = psql.Arg(s.ArcgisRefreshTokenExpires.MustGetNull())
		} else {
			vals[4] = psql.Raw("DEFAULT")
		}

		if !s.ArcgisRole.IsUnset() {
			vals[5] = psql.Arg(s.ArcgisRole.MustGetNull())
		} else {
			vals[5] = psql.Raw("DEFAULT")
		}

		if s.DisplayName.IsValue() {
			vals[6] = psql.Arg(s.DisplayName.MustGet())
		} else {
			vals[6] = psql.Raw("DEFAULT")
		}

		if !s.Email.IsUnset() {
			vals[7] = psql.Arg(s.Email.MustGetNull())
		} else {
			vals[7] = psql.Raw("DEFAULT")
		}

		if s.OrganizationID.IsValue() {
			vals[8] = psql.Arg(s.OrganizationID.MustGet())
		} else {
			vals[8] = psql.Raw("DEFAULT")
		}

		if s.Username.IsValue() {
			vals[9] = psql.Arg(s.Username.MustGet())
		} else {
			vals[9] = psql.Raw("DEFAULT")
		}

		if s.PasswordHashType.IsValue() {
			vals[10] = psql.Arg(s.PasswordHashType.MustGet())
		} else {
			vals[10] = psql.Raw("DEFAULT")
		}

		if s.PasswordHash.IsValue() {
			vals[11] = psql.Arg(s.PasswordHash.MustGet())
		} else {
			vals[11] = psql.Raw("DEFAULT")
		}

		if s.Role.IsValue() {
			vals[12] = psql.Arg(s.Role.MustGet())
		} else {
			vals[12] = psql.Raw("DEFAULT")
		}

		return bob.ExpressSlice(ctx, w, d, start, vals, "", ", ", "")
	}))
}

func (s UserSetter) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
	return um.Set(s.Expressions()...)
}

func (s UserSetter) Expressions(prefix ...string) []bob.Expression {
	exprs := make([]bob.Expression, 0, 13)

	if s.ID.IsValue() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			psql.Quote(append(prefix, "id")...),
			psql.Arg(s.ID),
		}})
	}

	if !s.ArcgisAccessToken.IsUnset() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			psql.Quote(append(prefix, "arcgis_access_token")...),
			psql.Arg(s.ArcgisAccessToken),
		}})
	}

	if !s.ArcgisLicense.IsUnset() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			psql.Quote(append(prefix, "arcgis_license")...),
			psql.Arg(s.ArcgisLicense),
		}})
	}

	if !s.ArcgisRefreshToken.IsUnset() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			psql.Quote(append(prefix, "arcgis_refresh_token")...),
			psql.Arg(s.ArcgisRefreshToken),
		}})
	}

	if !s.ArcgisRefreshTokenExpires.IsUnset() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			psql.Quote(append(prefix, "arcgis_refresh_token_expires")...),
			psql.Arg(s.ArcgisRefreshTokenExpires),
		}})
	}

	if !s.ArcgisRole.IsUnset() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			psql.Quote(append(prefix, "arcgis_role")...),
			psql.Arg(s.ArcgisRole),
		}})
	}

	if s.DisplayName.IsValue() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			psql.Quote(append(prefix, "display_name")...),
			psql.Arg(s.DisplayName),
		}})
	}

	if !s.Email.IsUnset() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			psql.Quote(append(prefix, "email")...),
			psql.Arg(s.Email),
		}})
	}

	if s.OrganizationID.IsValue() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			psql.Quote(append(prefix, "organization_id")...),
			psql.Arg(s.OrganizationID),
		}})
	}

	if s.Username.IsValue() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			psql.Quote(append(prefix, "username")...),
			psql.Arg(s.Username),
		}})
	}

	if s.PasswordHashType.IsValue() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			psql.Quote(append(prefix, "password_hash_type")...),
			psql.Arg(s.PasswordHashType),
		}})
	}

	if s.PasswordHash.IsValue() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			psql.Quote(append(prefix, "password_hash")...),
			psql.Arg(s.PasswordHash),
		}})
	}

	if s.Role.IsValue() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			psql.Quote(append(prefix, "role")...),
			psql.Arg(s.Role),
		}})
	}

	return exprs
}

// FindUser retrieves a single record by primary key
// If cols is empty Find will return all columns.
func FindUser(ctx context.Context, exec bob.Executor, IDPK int32, cols ...string) (*User, error) {
	if len(cols) == 0 {
		return Users.Query(
			sm.Where(Users.Columns.ID.EQ(psql.Arg(IDPK))),
		).One(ctx, exec)
	}

	return Users.Query(
		sm.Where(Users.Columns.ID.EQ(psql.Arg(IDPK))),
		sm.Columns(Users.Columns.Only(cols...)),
	).One(ctx, exec)
}

// UserExists checks the presence of a single record by primary key
func UserExists(ctx context.Context, exec bob.Executor, IDPK int32) (bool, error) {
	return Users.Query(
		sm.Where(Users.Columns.ID.EQ(psql.Arg(IDPK))),
	).Exists(ctx, exec)
}

// AfterQueryHook is called after User is retrieved from the database
func (o *User) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
	var err error

	switch queryType {
	case bob.QueryTypeSelect:
		ctx, err = Users.AfterSelectHooks.RunHooks(ctx, exec, UserSlice{o})
	case bob.QueryTypeInsert:
		ctx, err = Users.AfterInsertHooks.RunHooks(ctx, exec, UserSlice{o})
	case bob.QueryTypeUpdate:
		ctx, err = Users.AfterUpdateHooks.RunHooks(ctx, exec, UserSlice{o})
	case bob.QueryTypeDelete:
		ctx, err = Users.AfterDeleteHooks.RunHooks(ctx, exec, UserSlice{o})
	}

	return err
}

// primaryKeyVals returns the primary key values of the User
func (o *User) primaryKeyVals() bob.Expression {
	return psql.Arg(o.ID)
}

func (o *User) pkEQ() dialect.Expression {
	return psql.Quote("user_", "id").EQ(bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
		return o.primaryKeyVals().WriteSQL(ctx, w, d, start)
	}))
}

// Update uses an executor to update the User
func (o *User) Update(ctx context.Context, exec bob.Executor, s *UserSetter) error {
	v, err := Users.Update(s.UpdateMod(), um.Where(o.pkEQ())).One(ctx, exec)
	if err != nil {
		return err
	}

	o.R = v.R
	*o = *v

	return nil
}

// Delete deletes a single User record with an executor
func (o *User) Delete(ctx context.Context, exec bob.Executor) error {
	_, err := Users.Delete(dm.Where(o.pkEQ())).Exec(ctx, exec)
	return err
}

// Reload refreshes the User using the executor
func (o *User) Reload(ctx context.Context, exec bob.Executor) error {
	o2, err := Users.Query(
		sm.Where(Users.Columns.ID.EQ(psql.Arg(o.ID))),
	).One(ctx, exec)
	if err != nil {
		return err
	}
	o2.R = o.R
	*o = *o2

	return nil
}

// AfterQueryHook is called after UserSlice is retrieved from the database
func (o UserSlice) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
	var err error

	switch queryType {
	case bob.QueryTypeSelect:
		ctx, err = Users.AfterSelectHooks.RunHooks(ctx, exec, o)
	case bob.QueryTypeInsert:
		ctx, err = Users.AfterInsertHooks.RunHooks(ctx, exec, o)
	case bob.QueryTypeUpdate:
		ctx, err = Users.AfterUpdateHooks.RunHooks(ctx, exec, o)
	case bob.QueryTypeDelete:
		ctx, err = Users.AfterDeleteHooks.RunHooks(ctx, exec, o)
	}

	return err
}

func (o UserSlice) pkIN() dialect.Expression {
	if len(o) == 0 {
		return psql.Raw("NULL")
	}

	return psql.Quote("user_", "id").In(bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
		pkPairs := make([]bob.Expression, len(o))
		for i, row := range o {
			pkPairs[i] = row.primaryKeyVals()
		}
		return bob.ExpressSlice(ctx, w, d, start, pkPairs, "", ", ", "")
	}))
}

// copyMatchingRows finds models in the given slice that have the same primary key
// then it first copies the existing relationships from the old model to the new model
// and then replaces the old model in the slice with the new model
func (o UserSlice) copyMatchingRows(from ...*User) {
	for i, old := range o {
		for _, new := range from {
			if new.ID != old.ID {
				continue
			}
			new.R = old.R
			o[i] = new
			break
		}
	}
}

// UpdateMod modifies an update query with "WHERE primary_key IN (o...)"
func (o UserSlice) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
	return bob.ModFunc[*dialect.UpdateQuery](func(q *dialect.UpdateQuery) {
		q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
			return Users.BeforeUpdateHooks.RunHooks(ctx, exec, o)
		})

		q.AppendLoader(bob.LoaderFunc(func(ctx context.Context, exec bob.Executor, retrieved any) error {
			var err error
			switch retrieved := retrieved.(type) {
			case *User:
				o.copyMatchingRows(retrieved)
			case []*User:
				o.copyMatchingRows(retrieved...)
			case UserSlice:
				o.copyMatchingRows(retrieved...)
			default:
				// If the retrieved value is not a User or a slice of User
				// then run the AfterUpdateHooks on the slice
				_, err = Users.AfterUpdateHooks.RunHooks(ctx, exec, o)
			}

			return err
		}))

		q.AppendWhere(o.pkIN())
	})
}

// DeleteMod modifies an delete query with "WHERE primary_key IN (o...)"
func (o UserSlice) DeleteMod() bob.Mod[*dialect.DeleteQuery] {
	return bob.ModFunc[*dialect.DeleteQuery](func(q *dialect.DeleteQuery) {
		q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
			return Users.BeforeDeleteHooks.RunHooks(ctx, exec, o)
		})

		q.AppendLoader(bob.LoaderFunc(func(ctx context.Context, exec bob.Executor, retrieved any) error {
			var err error
			switch retrieved := retrieved.(type) {
			case *User:
				o.copyMatchingRows(retrieved)
			case []*User:
				o.copyMatchingRows(retrieved...)
			case UserSlice:
				o.copyMatchingRows(retrieved...)
			default:
				// If the retrieved value is not a User or a slice of User
				// then run the AfterDeleteHooks on the slice
				_, err = Users.AfterDeleteHooks.RunHooks(ctx, exec, o)
			}

			return err
		}))

		q.AppendWhere(o.pkIN())
	})
}

func (o UserSlice) UpdateAll(ctx context.Context, exec bob.Executor, vals UserSetter) error {
	if len(o) == 0 {
		return nil
	}

	_, err := Users.Update(vals.UpdateMod(), o.UpdateMod()).All(ctx, exec)
	return err
}

func (o UserSlice) DeleteAll(ctx context.Context, exec bob.Executor) error {
	if len(o) == 0 {
		return nil
	}

	_, err := Users.Delete(o.DeleteMod()).Exec(ctx, exec)
	return err
}

func (o UserSlice) ReloadAll(ctx context.Context, exec bob.Executor) error {
	if len(o) == 0 {
		return nil
	}

	o2, err := Users.Query(sm.Where(o.pkIN())).All(ctx, exec)
	if err != nil {
		return err
	}

	o.copyMatchingRows(o2...)

	return nil
}

// UserOauthTokens starts a query for related objects on arcgis.oauth_token
func (o *User) UserOauthTokens(mods ...bob.Mod[*dialect.SelectQuery]) ArcgisOauthTokensQuery {
	return ArcgisOauthTokens.Query(append(mods,
		sm.Where(ArcgisOauthTokens.Columns.UserID.EQ(psql.Arg(o.ID))),
	)...)
}

func (os UserSlice) UserOauthTokens(mods ...bob.Mod[*dialect.SelectQuery]) ArcgisOauthTokensQuery {
	pkID := make(pgtypes.Array[int32], 0, len(os))
	for _, o := range os {
		if o == nil {
			continue
		}
		pkID = append(pkID, o.ID)
	}
	PKArgExpr := psql.Select(sm.Columns(
		psql.F("unnest", psql.Cast(psql.Arg(pkID), "integer[]")),
	))

	return ArcgisOauthTokens.Query(append(mods,
		sm.Where(psql.Group(ArcgisOauthTokens.Columns.UserID).OP("IN", PKArgExpr)),
	)...)
}

// PublicUserUser starts a query for related objects on arcgis.user_
func (o *User) PublicUserUser(mods ...bob.Mod[*dialect.SelectQuery]) ArcgisUsersQuery {
	return ArcgisUsers.Query(append(mods,
		sm.Where(ArcgisUsers.Columns.PublicUserID.EQ(psql.Arg(o.ID))),
	)...)
}

func (os UserSlice) PublicUserUser(mods ...bob.Mod[*dialect.SelectQuery]) ArcgisUsersQuery {
	pkID := make(pgtypes.Array[int32], 0, len(os))
	for _, o := range os {
		if o == nil {
			continue
		}
		pkID = append(pkID, o.ID)
	}
	PKArgExpr := psql.Select(sm.Columns(
		psql.F("unnest", psql.Cast(psql.Arg(pkID), "integer[]")),
	))

	return ArcgisUsers.Query(append(mods,
		sm.Where(psql.Group(ArcgisUsers.Columns.PublicUserID).OP("IN", PKArgExpr)),
	)...)
}

// CreatorComplianceReportRequests starts a query for related objects on compliance_report_request
func (o *User) CreatorComplianceReportRequests(mods ...bob.Mod[*dialect.SelectQuery]) ComplianceReportRequestsQuery {
	return ComplianceReportRequests.Query(append(mods,
		sm.Where(ComplianceReportRequests.Columns.Creator.EQ(psql.Arg(o.ID))),
	)...)
}

func (os UserSlice) CreatorComplianceReportRequests(mods ...bob.Mod[*dialect.SelectQuery]) ComplianceReportRequestsQuery {
	pkID := make(pgtypes.Array[int32], 0, len(os))
	for _, o := range os {
		if o == nil {
			continue
		}
		pkID = append(pkID, o.ID)
	}
	PKArgExpr := psql.Select(sm.Columns(
		psql.F("unnest", psql.Cast(psql.Arg(pkID), "integer[]")),
	))

	return ComplianceReportRequests.Query(append(mods,
		sm.Where(psql.Group(ComplianceReportRequests.Columns.Creator).OP("IN", PKArgExpr)),
	)...)
}

// CreatorFiles starts a query for related objects on fileupload.file
func (o *User) CreatorFiles(mods ...bob.Mod[*dialect.SelectQuery]) FileuploadFilesQuery {
	return FileuploadFiles.Query(append(mods,
		sm.Where(FileuploadFiles.Columns.CreatorID.EQ(psql.Arg(o.ID))),
	)...)
}

func (os UserSlice) CreatorFiles(mods ...bob.Mod[*dialect.SelectQuery]) FileuploadFilesQuery {
	pkID := make(pgtypes.Array[int32], 0, len(os))
	for _, o := range os {
		if o == nil {
			continue
		}
		pkID = append(pkID, o.ID)
	}
	PKArgExpr := psql.Select(sm.Columns(
		psql.F("unnest", psql.Cast(psql.Arg(pkID), "integer[]")),
	))

	return FileuploadFiles.Query(append(mods,
		sm.Where(psql.Group(FileuploadFiles.Columns.CreatorID).OP("IN", PKArgExpr)),
	)...)
}

// FileuploadPool starts a query for related objects on fileupload.pool
func (o *User) FileuploadPool(mods ...bob.Mod[*dialect.SelectQuery]) FileuploadPoolsQuery {
	return FileuploadPools.Query(append(mods,
		sm.Where(FileuploadPools.Columns.CreatorID.EQ(psql.Arg(o.ID))),
	)...)
}

func (os UserSlice) FileuploadPool(mods ...bob.Mod[*dialect.SelectQuery]) FileuploadPoolsQuery {
	pkID := make(pgtypes.Array[int32], 0, len(os))
	for _, o := range os {
		if o == nil {
			continue
		}
		pkID = append(pkID, o.ID)
	}
	PKArgExpr := psql.Select(sm.Columns(
		psql.F("unnest", psql.Cast(psql.Arg(pkID), "integer[]")),
	))

	return FileuploadPools.Query(append(mods,
		sm.Where(psql.Group(FileuploadPools.Columns.CreatorID).OP("IN", PKArgExpr)),
	)...)
}

// CreatorNoteAudios starts a query for related objects on note_audio
func (o *User) CreatorNoteAudios(mods ...bob.Mod[*dialect.SelectQuery]) NoteAudiosQuery {
	return NoteAudios.Query(append(mods,
		sm.Where(NoteAudios.Columns.CreatorID.EQ(psql.Arg(o.ID))),
	)...)
}

func (os UserSlice) CreatorNoteAudios(mods ...bob.Mod[*dialect.SelectQuery]) NoteAudiosQuery {
	pkID := make(pgtypes.Array[int32], 0, len(os))
	for _, o := range os {
		if o == nil {
			continue
		}
		pkID = append(pkID, o.ID)
	}
	PKArgExpr := psql.Select(sm.Columns(
		psql.F("unnest", psql.Cast(psql.Arg(pkID), "integer[]")),
	))

	return NoteAudios.Query(append(mods,
		sm.Where(psql.Group(NoteAudios.Columns.CreatorID).OP("IN", PKArgExpr)),
	)...)
}

// DeletorNoteAudios starts a query for related objects on note_audio
func (o *User) DeletorNoteAudios(mods ...bob.Mod[*dialect.SelectQuery]) NoteAudiosQuery {
	return NoteAudios.Query(append(mods,
		sm.Where(NoteAudios.Columns.DeletorID.EQ(psql.Arg(o.ID))),
	)...)
}

func (os UserSlice) DeletorNoteAudios(mods ...bob.Mod[*dialect.SelectQuery]) NoteAudiosQuery {
	pkID := make(pgtypes.Array[int32], 0, len(os))
	for _, o := range os {
		if o == nil {
			continue
		}
		pkID = append(pkID, o.ID)
	}
	PKArgExpr := psql.Select(sm.Columns(
		psql.F("unnest", psql.Cast(psql.Arg(pkID), "integer[]")),
	))

	return NoteAudios.Query(append(mods,
		sm.Where(psql.Group(NoteAudios.Columns.DeletorID).OP("IN", PKArgExpr)),
	)...)
}

// CreatorNoteImages starts a query for related objects on note_image
func (o *User) CreatorNoteImages(mods ...bob.Mod[*dialect.SelectQuery]) NoteImagesQuery {
	return NoteImages.Query(append(mods,
		sm.Where(NoteImages.Columns.CreatorID.EQ(psql.Arg(o.ID))),
	)...)
}

func (os UserSlice) CreatorNoteImages(mods ...bob.Mod[*dialect.SelectQuery]) NoteImagesQuery {
	pkID := make(pgtypes.Array[int32], 0, len(os))
	for _, o := range os {
		if o == nil {
			continue
		}
		pkID = append(pkID, o.ID)
	}
	PKArgExpr := psql.Select(sm.Columns(
		psql.F("unnest", psql.Cast(psql.Arg(pkID), "integer[]")),
	))

	return NoteImages.Query(append(mods,
		sm.Where(psql.Group(NoteImages.Columns.CreatorID).OP("IN", PKArgExpr)),
	)...)
}

// DeletorNoteImages starts a query for related objects on note_image
func (o *User) DeletorNoteImages(mods ...bob.Mod[*dialect.SelectQuery]) NoteImagesQuery {
	return NoteImages.Query(append(mods,
		sm.Where(NoteImages.Columns.DeletorID.EQ(psql.Arg(o.ID))),
	)...)
}

func (os UserSlice) DeletorNoteImages(mods ...bob.Mod[*dialect.SelectQuery]) NoteImagesQuery {
	pkID := make(pgtypes.Array[int32], 0, len(os))
	for _, o := range os {
		if o == nil {
			continue
		}
		pkID = append(pkID, o.ID)
	}
	PKArgExpr := psql.Select(sm.Columns(
		psql.F("unnest", psql.Cast(psql.Arg(pkID), "integer[]")),
	))

	return NoteImages.Query(append(mods,
		sm.Where(psql.Group(NoteImages.Columns.DeletorID).OP("IN", PKArgExpr)),
	)...)
}

// UserNotifications starts a query for related objects on notification
func (o *User) UserNotifications(mods ...bob.Mod[*dialect.SelectQuery]) NotificationsQuery {
	return Notifications.Query(append(mods,
		sm.Where(Notifications.Columns.UserID.EQ(psql.Arg(o.ID))),
	)...)
}

func (os UserSlice) UserNotifications(mods ...bob.Mod[*dialect.SelectQuery]) NotificationsQuery {
	pkID := make(pgtypes.Array[int32], 0, len(os))
	for _, o := range os {
		if o == nil {
			continue
		}
		pkID = append(pkID, o.ID)
	}
	PKArgExpr := psql.Select(sm.Columns(
		psql.F("unnest", psql.Cast(psql.Arg(pkID), "integer[]")),
	))

	return Notifications.Query(append(mods,
		sm.Where(psql.Group(Notifications.Columns.UserID).OP("IN", PKArgExpr)),
	)...)
}

// CreatorPools starts a query for related objects on pool
func (o *User) CreatorPools(mods ...bob.Mod[*dialect.SelectQuery]) PoolsQuery {
	return Pools.Query(append(mods,
		sm.Where(Pools.Columns.CreatorID.EQ(psql.Arg(o.ID))),
	)...)
}

func (os UserSlice) CreatorPools(mods ...bob.Mod[*dialect.SelectQuery]) PoolsQuery {
	pkID := make(pgtypes.Array[int32], 0, len(os))
	for _, o := range os {
		if o == nil {
			continue
		}
		pkID = append(pkID, o.ID)
	}
	PKArgExpr := psql.Select(sm.Columns(
		psql.F("unnest", psql.Cast(psql.Arg(pkID), "integer[]")),
	))

	return Pools.Query(append(mods,
		sm.Where(psql.Group(Pools.Columns.CreatorID).OP("IN", PKArgExpr)),
	)...)
}

// CreatorResidents starts a query for related objects on resident
func (o *User) CreatorResidents(mods ...bob.Mod[*dialect.SelectQuery]) ResidentsQuery {
	return Residents.Query(append(mods,
		sm.Where(Residents.Columns.Creator.EQ(psql.Arg(o.ID))),
	)...)
}

func (os UserSlice) CreatorResidents(mods ...bob.Mod[*dialect.SelectQuery]) ResidentsQuery {
	pkID := make(pgtypes.Array[int32], 0, len(os))
	for _, o := range os {
		if o == nil {
			continue
		}
		pkID = append(pkID, o.ID)
	}
	PKArgExpr := psql.Select(sm.Columns(
		psql.F("unnest", psql.Cast(psql.Arg(pkID), "integer[]")),
	))

	return Residents.Query(append(mods,
		sm.Where(psql.Group(Residents.Columns.Creator).OP("IN", PKArgExpr)),
	)...)
}

// CreatorSites starts a query for related objects on site
func (o *User) CreatorSites(mods ...bob.Mod[*dialect.SelectQuery]) SitesQuery {
	return Sites.Query(append(mods,
		sm.Where(Sites.Columns.CreatorID.EQ(psql.Arg(o.ID))),
	)...)
}

func (os UserSlice) CreatorSites(mods ...bob.Mod[*dialect.SelectQuery]) SitesQuery {
	pkID := make(pgtypes.Array[int32], 0, len(os))
	for _, o := range os {
		if o == nil {
			continue
		}
		pkID = append(pkID, o.ID)
	}
	PKArgExpr := psql.Select(sm.Columns(
		psql.F("unnest", psql.Cast(psql.Arg(pkID), "integer[]")),
	))

	return Sites.Query(append(mods,
		sm.Where(psql.Group(Sites.Columns.CreatorID).OP("IN", PKArgExpr)),
	)...)
}

// Organization starts a query for related objects on organization
func (o *User) Organization(mods ...bob.Mod[*dialect.SelectQuery]) OrganizationsQuery {
	return Organizations.Query(append(mods,
		sm.Where(Organizations.Columns.ID.EQ(psql.Arg(o.OrganizationID))),
	)...)
}

func (os UserSlice) Organization(mods ...bob.Mod[*dialect.SelectQuery]) OrganizationsQuery {
	pkOrganizationID := make(pgtypes.Array[int32], 0, len(os))
	for _, o := range os {
		if o == nil {
			continue
		}
		pkOrganizationID = append(pkOrganizationID, o.OrganizationID)
	}
	PKArgExpr := psql.Select(sm.Columns(
		psql.F("unnest", psql.Cast(psql.Arg(pkOrganizationID), "integer[]")),
	))

	return Organizations.Query(append(mods,
		sm.Where(psql.Group(Organizations.Columns.ID).OP("IN", PKArgExpr)),
	)...)
}

func insertUserUserOauthTokens0(ctx context.Context, exec bob.Executor, arcgisOauthTokens1 []*ArcgisOauthTokenSetter, user0 *User) (ArcgisOauthTokenSlice, error) {
	for i := range arcgisOauthTokens1 {
		arcgisOauthTokens1[i].UserID = omit.From(user0.ID)
	}

	ret, err := ArcgisOauthTokens.Insert(bob.ToMods(arcgisOauthTokens1...)).All(ctx, exec)
	if err != nil {
		return ret, fmt.Errorf("insertUserUserOauthTokens0: %w", err)
	}

	return ret, nil
}

func attachUserUserOauthTokens0(ctx context.Context, exec bob.Executor, count int, arcgisOauthTokens1 ArcgisOauthTokenSlice, user0 *User) (ArcgisOauthTokenSlice, error) {
	setter := &ArcgisOauthTokenSetter{
		UserID: omit.From(user0.ID),
	}

	err := arcgisOauthTokens1.UpdateAll(ctx, exec, *setter)
	if err != nil {
		return nil, fmt.Errorf("attachUserUserOauthTokens0: %w", err)
	}

	return arcgisOauthTokens1, nil
}

func (user0 *User) InsertUserOauthTokens(ctx context.Context, exec bob.Executor, related ...*ArcgisOauthTokenSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	arcgisOauthTokens1, err := insertUserUserOauthTokens0(ctx, exec, related, user0)
	if err != nil {
		return err
	}

	user0.R.UserOauthTokens = append(user0.R.UserOauthTokens, arcgisOauthTokens1...)

	for _, rel := range arcgisOauthTokens1 {
		rel.R.UserUser = user0
	}
	return nil
}

func (user0 *User) AttachUserOauthTokens(ctx context.Context, exec bob.Executor, related ...*ArcgisOauthToken) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	arcgisOauthTokens1 := ArcgisOauthTokenSlice(related)

	_, err = attachUserUserOauthTokens0(ctx, exec, len(related), arcgisOauthTokens1, user0)
	if err != nil {
		return err
	}

	user0.R.UserOauthTokens = append(user0.R.UserOauthTokens, arcgisOauthTokens1...)

	for _, rel := range related {
		rel.R.UserUser = user0
	}

	return nil
}

func insertUserPublicUserUser0(ctx context.Context, exec bob.Executor, arcgisusers1 []*ArcgisUserSetter, user0 *User) (ArcgisUserSlice, error) {
	for i := range arcgisusers1 {
		arcgisusers1[i].PublicUserID = omit.From(user0.ID)
	}

	ret, err := ArcgisUsers.Insert(bob.ToMods(arcgisusers1...)).All(ctx, exec)
	if err != nil {
		return ret, fmt.Errorf("insertUserPublicUserUser0: %w", err)
	}

	return ret, nil
}

func attachUserPublicUserUser0(ctx context.Context, exec bob.Executor, count int, arcgisusers1 ArcgisUserSlice, user0 *User) (ArcgisUserSlice, error) {
	setter := &ArcgisUserSetter{
		PublicUserID: omit.From(user0.ID),
	}

	err := arcgisusers1.UpdateAll(ctx, exec, *setter)
	if err != nil {
		return nil, fmt.Errorf("attachUserPublicUserUser0: %w", err)
	}

	return arcgisusers1, nil
}

func (user0 *User) InsertPublicUserUser(ctx context.Context, exec bob.Executor, related ...*ArcgisUserSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	arcgisusers1, err := insertUserPublicUserUser0(ctx, exec, related, user0)
	if err != nil {
		return err
	}

	user0.R.PublicUserUser = append(user0.R.PublicUserUser, arcgisusers1...)

	for _, rel := range arcgisusers1 {
		rel.R.PublicUserUser = user0
	}
	return nil
}

func (user0 *User) AttachPublicUserUser(ctx context.Context, exec bob.Executor, related ...*ArcgisUser) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	arcgisusers1 := ArcgisUserSlice(related)

	_, err = attachUserPublicUserUser0(ctx, exec, len(related), arcgisusers1, user0)
	if err != nil {
		return err
	}

	user0.R.PublicUserUser = append(user0.R.PublicUserUser, arcgisusers1...)

	for _, rel := range related {
		rel.R.PublicUserUser = user0
	}

	return nil
}

func insertUserCreatorComplianceReportRequests0(ctx context.Context, exec bob.Executor, complianceReportRequests1 []*ComplianceReportRequestSetter, user0 *User) (ComplianceReportRequestSlice, error) {
	for i := range complianceReportRequests1 {
		complianceReportRequests1[i].Creator = omit.From(user0.ID)
	}

	ret, err := ComplianceReportRequests.Insert(bob.ToMods(complianceReportRequests1...)).All(ctx, exec)
	if err != nil {
		return ret, fmt.Errorf("insertUserCreatorComplianceReportRequests0: %w", err)
	}

	return ret, nil
}

func attachUserCreatorComplianceReportRequests0(ctx context.Context, exec bob.Executor, count int, complianceReportRequests1 ComplianceReportRequestSlice, user0 *User) (ComplianceReportRequestSlice, error) {
	setter := &ComplianceReportRequestSetter{
		Creator: omit.From(user0.ID),
	}

	err := complianceReportRequests1.UpdateAll(ctx, exec, *setter)
	if err != nil {
		return nil, fmt.Errorf("attachUserCreatorComplianceReportRequests0: %w", err)
	}

	return complianceReportRequests1, nil
}

func (user0 *User) InsertCreatorComplianceReportRequests(ctx context.Context, exec bob.Executor, related ...*ComplianceReportRequestSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	complianceReportRequests1, err := insertUserCreatorComplianceReportRequests0(ctx, exec, related, user0)
	if err != nil {
		return err
	}

	user0.R.CreatorComplianceReportRequests = append(user0.R.CreatorComplianceReportRequests, complianceReportRequests1...)

	for _, rel := range complianceReportRequests1 {
		rel.R.CreatorUser = user0
	}
	return nil
}

func (user0 *User) AttachCreatorComplianceReportRequests(ctx context.Context, exec bob.Executor, related ...*ComplianceReportRequest) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	complianceReportRequests1 := ComplianceReportRequestSlice(related)

	_, err = attachUserCreatorComplianceReportRequests0(ctx, exec, len(related), complianceReportRequests1, user0)
	if err != nil {
		return err
	}

	user0.R.CreatorComplianceReportRequests = append(user0.R.CreatorComplianceReportRequests, complianceReportRequests1...)

	for _, rel := range related {
		rel.R.CreatorUser = user0
	}

	return nil
}

func insertUserCreatorFiles0(ctx context.Context, exec bob.Executor, fileuploadFiles1 []*FileuploadFileSetter, user0 *User) (FileuploadFileSlice, error) {
	for i := range fileuploadFiles1 {
		fileuploadFiles1[i].CreatorID = omit.From(user0.ID)
	}

	ret, err := FileuploadFiles.Insert(bob.ToMods(fileuploadFiles1...)).All(ctx, exec)
	if err != nil {
		return ret, fmt.Errorf("insertUserCreatorFiles0: %w", err)
	}

	return ret, nil
}

func attachUserCreatorFiles0(ctx context.Context, exec bob.Executor, count int, fileuploadFiles1 FileuploadFileSlice, user0 *User) (FileuploadFileSlice, error) {
	setter := &FileuploadFileSetter{
		CreatorID: omit.From(user0.ID),
	}

	err := fileuploadFiles1.UpdateAll(ctx, exec, *setter)
	if err != nil {
		return nil, fmt.Errorf("attachUserCreatorFiles0: %w", err)
	}

	return fileuploadFiles1, nil
}

func (user0 *User) InsertCreatorFiles(ctx context.Context, exec bob.Executor, related ...*FileuploadFileSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	fileuploadFiles1, err := insertUserCreatorFiles0(ctx, exec, related, user0)
	if err != nil {
		return err
	}

	user0.R.CreatorFiles = append(user0.R.CreatorFiles, fileuploadFiles1...)

	for _, rel := range fileuploadFiles1 {
		rel.R.CreatorUser = user0
	}
	return nil
}

func (user0 *User) AttachCreatorFiles(ctx context.Context, exec bob.Executor, related ...*FileuploadFile) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	fileuploadFiles1 := FileuploadFileSlice(related)

	_, err = attachUserCreatorFiles0(ctx, exec, len(related), fileuploadFiles1, user0)
	if err != nil {
		return err
	}

	user0.R.CreatorFiles = append(user0.R.CreatorFiles, fileuploadFiles1...)

	for _, rel := range related {
		rel.R.CreatorUser = user0
	}

	return nil
}

func insertUserFileuploadPool0(ctx context.Context, exec bob.Executor, fileuploadPools1 []*FileuploadPoolSetter, user0 *User) (FileuploadPoolSlice, error) {
	for i := range fileuploadPools1 {
		fileuploadPools1[i].CreatorID = omit.From(user0.ID)
	}

	ret, err := FileuploadPools.Insert(bob.ToMods(fileuploadPools1...)).All(ctx, exec)
	if err != nil {
		return ret, fmt.Errorf("insertUserFileuploadPool0: %w", err)
	}

	return ret, nil
}

func attachUserFileuploadPool0(ctx context.Context, exec bob.Executor, count int, fileuploadPools1 FileuploadPoolSlice, user0 *User) (FileuploadPoolSlice, error) {
	setter := &FileuploadPoolSetter{
		CreatorID: omit.From(user0.ID),
	}

	err := fileuploadPools1.UpdateAll(ctx, exec, *setter)
	if err != nil {
		return nil, fmt.Errorf("attachUserFileuploadPool0: %w", err)
	}

	return fileuploadPools1, nil
}

func (user0 *User) InsertFileuploadPool(ctx context.Context, exec bob.Executor, related ...*FileuploadPoolSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	fileuploadPools1, err := insertUserFileuploadPool0(ctx, exec, related, user0)
	if err != nil {
		return err
	}

	user0.R.FileuploadPool = append(user0.R.FileuploadPool, fileuploadPools1...)

	for _, rel := range fileuploadPools1 {
		rel.R.CreatorUser = user0
	}
	return nil
}

func (user0 *User) AttachFileuploadPool(ctx context.Context, exec bob.Executor, related ...*FileuploadPool) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	fileuploadPools1 := FileuploadPoolSlice(related)

	_, err = attachUserFileuploadPool0(ctx, exec, len(related), fileuploadPools1, user0)
	if err != nil {
		return err
	}

	user0.R.FileuploadPool = append(user0.R.FileuploadPool, fileuploadPools1...)

	for _, rel := range related {
		rel.R.CreatorUser = user0
	}

	return nil
}

func insertUserCreatorNoteAudios0(ctx context.Context, exec bob.Executor, noteAudios1 []*NoteAudioSetter, user0 *User) (NoteAudioSlice, error) {
	for i := range noteAudios1 {
		noteAudios1[i].CreatorID = omit.From(user0.ID)
	}

	ret, err := NoteAudios.Insert(bob.ToMods(noteAudios1...)).All(ctx, exec)
	if err != nil {
		return ret, fmt.Errorf("insertUserCreatorNoteAudios0: %w", err)
	}

	return ret, nil
}

func attachUserCreatorNoteAudios0(ctx context.Context, exec bob.Executor, count int, noteAudios1 NoteAudioSlice, user0 *User) (NoteAudioSlice, error) {
	setter := &NoteAudioSetter{
		CreatorID: omit.From(user0.ID),
	}

	err := noteAudios1.UpdateAll(ctx, exec, *setter)
	if err != nil {
		return nil, fmt.Errorf("attachUserCreatorNoteAudios0: %w", err)
	}

	return noteAudios1, nil
}

func (user0 *User) InsertCreatorNoteAudios(ctx context.Context, exec bob.Executor, related ...*NoteAudioSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	noteAudios1, err := insertUserCreatorNoteAudios0(ctx, exec, related, user0)
	if err != nil {
		return err
	}

	user0.R.CreatorNoteAudios = append(user0.R.CreatorNoteAudios, noteAudios1...)

	for _, rel := range noteAudios1 {
		rel.R.CreatorUser = user0
	}
	return nil
}

func (user0 *User) AttachCreatorNoteAudios(ctx context.Context, exec bob.Executor, related ...*NoteAudio) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	noteAudios1 := NoteAudioSlice(related)

	_, err = attachUserCreatorNoteAudios0(ctx, exec, len(related), noteAudios1, user0)
	if err != nil {
		return err
	}

	user0.R.CreatorNoteAudios = append(user0.R.CreatorNoteAudios, noteAudios1...)

	for _, rel := range related {
		rel.R.CreatorUser = user0
	}

	return nil
}

func insertUserDeletorNoteAudios0(ctx context.Context, exec bob.Executor, noteAudios1 []*NoteAudioSetter, user0 *User) (NoteAudioSlice, error) {
	for i := range noteAudios1 {
		noteAudios1[i].DeletorID = omitnull.From(user0.ID)
	}

	ret, err := NoteAudios.Insert(bob.ToMods(noteAudios1...)).All(ctx, exec)
	if err != nil {
		return ret, fmt.Errorf("insertUserDeletorNoteAudios0: %w", err)
	}

	return ret, nil
}

func attachUserDeletorNoteAudios0(ctx context.Context, exec bob.Executor, count int, noteAudios1 NoteAudioSlice, user0 *User) (NoteAudioSlice, error) {
	setter := &NoteAudioSetter{
		DeletorID: omitnull.From(user0.ID),
	}

	err := noteAudios1.UpdateAll(ctx, exec, *setter)
	if err != nil {
		return nil, fmt.Errorf("attachUserDeletorNoteAudios0: %w", err)
	}

	return noteAudios1, nil
}

func (user0 *User) InsertDeletorNoteAudios(ctx context.Context, exec bob.Executor, related ...*NoteAudioSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	noteAudios1, err := insertUserDeletorNoteAudios0(ctx, exec, related, user0)
	if err != nil {
		return err
	}

	user0.R.DeletorNoteAudios = append(user0.R.DeletorNoteAudios, noteAudios1...)

	for _, rel := range noteAudios1 {
		rel.R.DeletorUser = user0
	}
	return nil
}

func (user0 *User) AttachDeletorNoteAudios(ctx context.Context, exec bob.Executor, related ...*NoteAudio) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	noteAudios1 := NoteAudioSlice(related)

	_, err = attachUserDeletorNoteAudios0(ctx, exec, len(related), noteAudios1, user0)
	if err != nil {
		return err
	}

	user0.R.DeletorNoteAudios = append(user0.R.DeletorNoteAudios, noteAudios1...)

	for _, rel := range related {
		rel.R.DeletorUser = user0
	}

	return nil
}

func insertUserCreatorNoteImages0(ctx context.Context, exec bob.Executor, noteImages1 []*NoteImageSetter, user0 *User) (NoteImageSlice, error) {
	for i := range noteImages1 {
		noteImages1[i].CreatorID = omit.From(user0.ID)
	}

	ret, err := NoteImages.Insert(bob.ToMods(noteImages1...)).All(ctx, exec)
	if err != nil {
		return ret, fmt.Errorf("insertUserCreatorNoteImages0: %w", err)
	}

	return ret, nil
}

func attachUserCreatorNoteImages0(ctx context.Context, exec bob.Executor, count int, noteImages1 NoteImageSlice, user0 *User) (NoteImageSlice, error) {
	setter := &NoteImageSetter{
		CreatorID: omit.From(user0.ID),
	}

	err := noteImages1.UpdateAll(ctx, exec, *setter)
	if err != nil {
		return nil, fmt.Errorf("attachUserCreatorNoteImages0: %w", err)
	}

	return noteImages1, nil
}

func (user0 *User) InsertCreatorNoteImages(ctx context.Context, exec bob.Executor, related ...*NoteImageSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	noteImages1, err := insertUserCreatorNoteImages0(ctx, exec, related, user0)
	if err != nil {
		return err
	}

	user0.R.CreatorNoteImages = append(user0.R.CreatorNoteImages, noteImages1...)

	for _, rel := range noteImages1 {
		rel.R.CreatorUser = user0
	}
	return nil
}

func (user0 *User) AttachCreatorNoteImages(ctx context.Context, exec bob.Executor, related ...*NoteImage) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	noteImages1 := NoteImageSlice(related)

	_, err = attachUserCreatorNoteImages0(ctx, exec, len(related), noteImages1, user0)
	if err != nil {
		return err
	}

	user0.R.CreatorNoteImages = append(user0.R.CreatorNoteImages, noteImages1...)

	for _, rel := range related {
		rel.R.CreatorUser = user0
	}

	return nil
}

func insertUserDeletorNoteImages0(ctx context.Context, exec bob.Executor, noteImages1 []*NoteImageSetter, user0 *User) (NoteImageSlice, error) {
	for i := range noteImages1 {
		noteImages1[i].DeletorID = omitnull.From(user0.ID)
	}

	ret, err := NoteImages.Insert(bob.ToMods(noteImages1...)).All(ctx, exec)
	if err != nil {
		return ret, fmt.Errorf("insertUserDeletorNoteImages0: %w", err)
	}

	return ret, nil
}

func attachUserDeletorNoteImages0(ctx context.Context, exec bob.Executor, count int, noteImages1 NoteImageSlice, user0 *User) (NoteImageSlice, error) {
	setter := &NoteImageSetter{
		DeletorID: omitnull.From(user0.ID),
	}

	err := noteImages1.UpdateAll(ctx, exec, *setter)
	if err != nil {
		return nil, fmt.Errorf("attachUserDeletorNoteImages0: %w", err)
	}

	return noteImages1, nil
}

func (user0 *User) InsertDeletorNoteImages(ctx context.Context, exec bob.Executor, related ...*NoteImageSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	noteImages1, err := insertUserDeletorNoteImages0(ctx, exec, related, user0)
	if err != nil {
		return err
	}

	user0.R.DeletorNoteImages = append(user0.R.DeletorNoteImages, noteImages1...)

	for _, rel := range noteImages1 {
		rel.R.DeletorUser = user0
	}
	return nil
}

func (user0 *User) AttachDeletorNoteImages(ctx context.Context, exec bob.Executor, related ...*NoteImage) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	noteImages1 := NoteImageSlice(related)

	_, err = attachUserDeletorNoteImages0(ctx, exec, len(related), noteImages1, user0)
	if err != nil {
		return err
	}

	user0.R.DeletorNoteImages = append(user0.R.DeletorNoteImages, noteImages1...)

	for _, rel := range related {
		rel.R.DeletorUser = user0
	}

	return nil
}

func insertUserUserNotifications0(ctx context.Context, exec bob.Executor, notifications1 []*NotificationSetter, user0 *User) (NotificationSlice, error) {
	for i := range notifications1 {
		notifications1[i].UserID = omit.From(user0.ID)
	}

	ret, err := Notifications.Insert(bob.ToMods(notifications1...)).All(ctx, exec)
	if err != nil {
		return ret, fmt.Errorf("insertUserUserNotifications0: %w", err)
	}

	return ret, nil
}

func attachUserUserNotifications0(ctx context.Context, exec bob.Executor, count int, notifications1 NotificationSlice, user0 *User) (NotificationSlice, error) {
	setter := &NotificationSetter{
		UserID: omit.From(user0.ID),
	}

	err := notifications1.UpdateAll(ctx, exec, *setter)
	if err != nil {
		return nil, fmt.Errorf("attachUserUserNotifications0: %w", err)
	}

	return notifications1, nil
}

func (user0 *User) InsertUserNotifications(ctx context.Context, exec bob.Executor, related ...*NotificationSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	notifications1, err := insertUserUserNotifications0(ctx, exec, related, user0)
	if err != nil {
		return err
	}

	user0.R.UserNotifications = append(user0.R.UserNotifications, notifications1...)

	for _, rel := range notifications1 {
		rel.R.UserUser = user0
	}
	return nil
}

func (user0 *User) AttachUserNotifications(ctx context.Context, exec bob.Executor, related ...*Notification) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	notifications1 := NotificationSlice(related)

	_, err = attachUserUserNotifications0(ctx, exec, len(related), notifications1, user0)
	if err != nil {
		return err
	}

	user0.R.UserNotifications = append(user0.R.UserNotifications, notifications1...)

	for _, rel := range related {
		rel.R.UserUser = user0
	}

	return nil
}

func insertUserCreatorPools0(ctx context.Context, exec bob.Executor, pools1 []*PoolSetter, user0 *User) (PoolSlice, error) {
	for i := range pools1 {
		pools1[i].CreatorID = omit.From(user0.ID)
	}

	ret, err := Pools.Insert(bob.ToMods(pools1...)).All(ctx, exec)
	if err != nil {
		return ret, fmt.Errorf("insertUserCreatorPools0: %w", err)
	}

	return ret, nil
}

func attachUserCreatorPools0(ctx context.Context, exec bob.Executor, count int, pools1 PoolSlice, user0 *User) (PoolSlice, error) {
	setter := &PoolSetter{
		CreatorID: omit.From(user0.ID),
	}

	err := pools1.UpdateAll(ctx, exec, *setter)
	if err != nil {
		return nil, fmt.Errorf("attachUserCreatorPools0: %w", err)
	}

	return pools1, nil
}

func (user0 *User) InsertCreatorPools(ctx context.Context, exec bob.Executor, related ...*PoolSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	pools1, err := insertUserCreatorPools0(ctx, exec, related, user0)
	if err != nil {
		return err
	}

	user0.R.CreatorPools = append(user0.R.CreatorPools, pools1...)

	for _, rel := range pools1 {
		rel.R.CreatorUser = user0
	}
	return nil
}

func (user0 *User) AttachCreatorPools(ctx context.Context, exec bob.Executor, related ...*Pool) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	pools1 := PoolSlice(related)

	_, err = attachUserCreatorPools0(ctx, exec, len(related), pools1, user0)
	if err != nil {
		return err
	}

	user0.R.CreatorPools = append(user0.R.CreatorPools, pools1...)

	for _, rel := range related {
		rel.R.CreatorUser = user0
	}

	return nil
}

func insertUserCreatorResidents0(ctx context.Context, exec bob.Executor, residents1 []*ResidentSetter, user0 *User) (ResidentSlice, error) {
	for i := range residents1 {
		residents1[i].Creator = omit.From(user0.ID)
	}

	ret, err := Residents.Insert(bob.ToMods(residents1...)).All(ctx, exec)
	if err != nil {
		return ret, fmt.Errorf("insertUserCreatorResidents0: %w", err)
	}

	return ret, nil
}

func attachUserCreatorResidents0(ctx context.Context, exec bob.Executor, count int, residents1 ResidentSlice, user0 *User) (ResidentSlice, error) {
	setter := &ResidentSetter{
		Creator: omit.From(user0.ID),
	}

	err := residents1.UpdateAll(ctx, exec, *setter)
	if err != nil {
		return nil, fmt.Errorf("attachUserCreatorResidents0: %w", err)
	}

	return residents1, nil
}

func (user0 *User) InsertCreatorResidents(ctx context.Context, exec bob.Executor, related ...*ResidentSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	residents1, err := insertUserCreatorResidents0(ctx, exec, related, user0)
	if err != nil {
		return err
	}

	user0.R.CreatorResidents = append(user0.R.CreatorResidents, residents1...)

	for _, rel := range residents1 {
		rel.R.CreatorUser = user0
	}
	return nil
}

func (user0 *User) AttachCreatorResidents(ctx context.Context, exec bob.Executor, related ...*Resident) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	residents1 := ResidentSlice(related)

	_, err = attachUserCreatorResidents0(ctx, exec, len(related), residents1, user0)
	if err != nil {
		return err
	}

	user0.R.CreatorResidents = append(user0.R.CreatorResidents, residents1...)

	for _, rel := range related {
		rel.R.CreatorUser = user0
	}

	return nil
}

func insertUserCreatorSites0(ctx context.Context, exec bob.Executor, sites1 []*SiteSetter, user0 *User) (SiteSlice, error) {
	for i := range sites1 {
		sites1[i].CreatorID = omit.From(user0.ID)
	}

	ret, err := Sites.Insert(bob.ToMods(sites1...)).All(ctx, exec)
	if err != nil {
		return ret, fmt.Errorf("insertUserCreatorSites0: %w", err)
	}

	return ret, nil
}

func attachUserCreatorSites0(ctx context.Context, exec bob.Executor, count int, sites1 SiteSlice, user0 *User) (SiteSlice, error) {
	setter := &SiteSetter{
		CreatorID: omit.From(user0.ID),
	}

	err := sites1.UpdateAll(ctx, exec, *setter)
	if err != nil {
		return nil, fmt.Errorf("attachUserCreatorSites0: %w", err)
	}

	return sites1, nil
}

func (user0 *User) InsertCreatorSites(ctx context.Context, exec bob.Executor, related ...*SiteSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	sites1, err := insertUserCreatorSites0(ctx, exec, related, user0)
	if err != nil {
		return err
	}

	user0.R.CreatorSites = append(user0.R.CreatorSites, sites1...)

	for _, rel := range sites1 {
		rel.R.CreatorUser = user0
	}
	return nil
}

func (user0 *User) AttachCreatorSites(ctx context.Context, exec bob.Executor, related ...*Site) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	sites1 := SiteSlice(related)

	_, err = attachUserCreatorSites0(ctx, exec, len(related), sites1, user0)
	if err != nil {
		return err
	}

	user0.R.CreatorSites = append(user0.R.CreatorSites, sites1...)

	for _, rel := range related {
		rel.R.CreatorUser = user0
	}

	return nil
}

func attachUserOrganization0(ctx context.Context, exec bob.Executor, count int, user0 *User, organization1 *Organization) (*User, error) {
	setter := &UserSetter{
		OrganizationID: omit.From(organization1.ID),
	}

	err := user0.Update(ctx, exec, setter)
	if err != nil {
		return nil, fmt.Errorf("attachUserOrganization0: %w", err)
	}

	return user0, nil
}

func (user0 *User) InsertOrganization(ctx context.Context, exec bob.Executor, related *OrganizationSetter) error {
	var err error

	organization1, err := Organizations.Insert(related).One(ctx, exec)
	if err != nil {
		return fmt.Errorf("inserting related objects: %w", err)
	}

	_, err = attachUserOrganization0(ctx, exec, 1, user0, organization1)
	if err != nil {
		return err
	}

	user0.R.Organization = organization1

	organization1.R.User = append(organization1.R.User, user0)

	return nil
}

func (user0 *User) AttachOrganization(ctx context.Context, exec bob.Executor, organization1 *Organization) error {
	var err error

	_, err = attachUserOrganization0(ctx, exec, 1, user0, organization1)
	if err != nil {
		return err
	}

	user0.R.Organization = organization1

	organization1.R.User = append(organization1.R.User, user0)

	return nil
}

type userWhere[Q psql.Filterable] struct {
	ID                        psql.WhereMod[Q, int32]
	ArcgisAccessToken         psql.WhereNullMod[Q, string]
	ArcgisLicense             psql.WhereNullMod[Q, enums.Arcgislicensetype]
	ArcgisRefreshToken        psql.WhereNullMod[Q, string]
	ArcgisRefreshTokenExpires psql.WhereNullMod[Q, time.Time]
	ArcgisRole                psql.WhereNullMod[Q, string]
	DisplayName               psql.WhereMod[Q, string]
	Email                     psql.WhereNullMod[Q, string]
	OrganizationID            psql.WhereMod[Q, int32]
	Username                  psql.WhereMod[Q, string]
	PasswordHashType          psql.WhereMod[Q, enums.Hashtype]
	PasswordHash              psql.WhereMod[Q, string]
	Role                      psql.WhereMod[Q, enums.Userrole]
}

func (userWhere[Q]) AliasedAs(alias string) userWhere[Q] {
	return buildUserWhere[Q](buildUserColumns(alias))
}

func buildUserWhere[Q psql.Filterable](cols userColumns) userWhere[Q] {
	return userWhere[Q]{
		ID:                        psql.Where[Q, int32](cols.ID),
		ArcgisAccessToken:         psql.WhereNull[Q, string](cols.ArcgisAccessToken),
		ArcgisLicense:             psql.WhereNull[Q, enums.Arcgislicensetype](cols.ArcgisLicense),
		ArcgisRefreshToken:        psql.WhereNull[Q, string](cols.ArcgisRefreshToken),
		ArcgisRefreshTokenExpires: psql.WhereNull[Q, time.Time](cols.ArcgisRefreshTokenExpires),
		ArcgisRole:                psql.WhereNull[Q, string](cols.ArcgisRole),
		DisplayName:               psql.Where[Q, string](cols.DisplayName),
		Email:                     psql.WhereNull[Q, string](cols.Email),
		OrganizationID:            psql.Where[Q, int32](cols.OrganizationID),
		Username:                  psql.Where[Q, string](cols.Username),
		PasswordHashType:          psql.Where[Q, enums.Hashtype](cols.PasswordHashType),
		PasswordHash:              psql.Where[Q, string](cols.PasswordHash),
		Role:                      psql.Where[Q, enums.Userrole](cols.Role),
	}
}

func (o *User) Preload(name string, retrieved any) error {
	if o == nil {
		return nil
	}

	switch name {
	case "UserOauthTokens":
		rels, ok := retrieved.(ArcgisOauthTokenSlice)
		if !ok {
			return fmt.Errorf("user cannot load %T as %q", retrieved, name)
		}

		o.R.UserOauthTokens = rels

		for _, rel := range rels {
			if rel != nil {
				rel.R.UserUser = o
			}
		}
		return nil
	case "PublicUserUser":
		rels, ok := retrieved.(ArcgisUserSlice)
		if !ok {
			return fmt.Errorf("user cannot load %T as %q", retrieved, name)
		}

		o.R.PublicUserUser = rels

		for _, rel := range rels {
			if rel != nil {
				rel.R.PublicUserUser = o
			}
		}
		return nil
	case "CreatorComplianceReportRequests":
		rels, ok := retrieved.(ComplianceReportRequestSlice)
		if !ok {
			return fmt.Errorf("user cannot load %T as %q", retrieved, name)
		}

		o.R.CreatorComplianceReportRequests = rels

		for _, rel := range rels {
			if rel != nil {
				rel.R.CreatorUser = o
			}
		}
		return nil
	case "CreatorFiles":
		rels, ok := retrieved.(FileuploadFileSlice)
		if !ok {
			return fmt.Errorf("user cannot load %T as %q", retrieved, name)
		}

		o.R.CreatorFiles = rels

		for _, rel := range rels {
			if rel != nil {
				rel.R.CreatorUser = o
			}
		}
		return nil
	case "FileuploadPool":
		rels, ok := retrieved.(FileuploadPoolSlice)
		if !ok {
			return fmt.Errorf("user cannot load %T as %q", retrieved, name)
		}

		o.R.FileuploadPool = rels

		for _, rel := range rels {
			if rel != nil {
				rel.R.CreatorUser = o
			}
		}
		return nil
	case "CreatorNoteAudios":
		rels, ok := retrieved.(NoteAudioSlice)
		if !ok {
			return fmt.Errorf("user cannot load %T as %q", retrieved, name)
		}

		o.R.CreatorNoteAudios = rels

		for _, rel := range rels {
			if rel != nil {
				rel.R.CreatorUser = o
			}
		}
		return nil
	case "DeletorNoteAudios":
		rels, ok := retrieved.(NoteAudioSlice)
		if !ok {
			return fmt.Errorf("user cannot load %T as %q", retrieved, name)
		}

		o.R.DeletorNoteAudios = rels

		for _, rel := range rels {
			if rel != nil {
				rel.R.DeletorUser = o
			}
		}
		return nil
	case "CreatorNoteImages":
		rels, ok := retrieved.(NoteImageSlice)
		if !ok {
			return fmt.Errorf("user cannot load %T as %q", retrieved, name)
		}

		o.R.CreatorNoteImages = rels

		for _, rel := range rels {
			if rel != nil {
				rel.R.CreatorUser = o
			}
		}
		return nil
	case "DeletorNoteImages":
		rels, ok := retrieved.(NoteImageSlice)
		if !ok {
			return fmt.Errorf("user cannot load %T as %q", retrieved, name)
		}

		o.R.DeletorNoteImages = rels

		for _, rel := range rels {
			if rel != nil {
				rel.R.DeletorUser = o
			}
		}
		return nil
	case "UserNotifications":
		rels, ok := retrieved.(NotificationSlice)
		if !ok {
			return fmt.Errorf("user cannot load %T as %q", retrieved, name)
		}

		o.R.UserNotifications = rels

		for _, rel := range rels {
			if rel != nil {
				rel.R.UserUser = o
			}
		}
		return nil
	case "CreatorPools":
		rels, ok := retrieved.(PoolSlice)
		if !ok {
			return fmt.Errorf("user cannot load %T as %q", retrieved, name)
		}

		o.R.CreatorPools = rels

		for _, rel := range rels {
			if rel != nil {
				rel.R.CreatorUser = o
			}
		}
		return nil
	case "CreatorResidents":
		rels, ok := retrieved.(ResidentSlice)
		if !ok {
			return fmt.Errorf("user cannot load %T as %q", retrieved, name)
		}

		o.R.CreatorResidents = rels

		for _, rel := range rels {
			if rel != nil {
				rel.R.CreatorUser = o
			}
		}
		return nil
	case "CreatorSites":
		rels, ok := retrieved.(SiteSlice)
		if !ok {
			return fmt.Errorf("user cannot load %T as %q", retrieved, name)
		}

		o.R.CreatorSites = rels

		for _, rel := range rels {
			if rel != nil {
				rel.R.CreatorUser = o
			}
		}
		return nil
	case "Organization":
		rel, ok := retrieved.(*Organization)
		if !ok {
			return fmt.Errorf("user cannot load %T as %q", retrieved, name)
		}

		o.R.Organization = rel

		if rel != nil {
			rel.R.User = UserSlice{o}
		}
		return nil
	default:
		return fmt.Errorf("user has no relationship %q", name)
	}
}

type userPreloader struct {
	Organization func(...psql.PreloadOption) psql.Preloader
}

func buildUserPreloader() userPreloader {
	return userPreloader{
		Organization: func(opts ...psql.PreloadOption) psql.Preloader {
			return psql.Preload[*Organization, OrganizationSlice](psql.PreloadRel{
				Name: "Organization",
				Sides: []psql.PreloadSide{
					{
						From:        Users,
						To:          Organizations,
						FromColumns: []string{"organization_id"},
						ToColumns:   []string{"id"},
					},
				},
			}, Organizations.Columns.Names(), opts...)
		},
	}
}

type userThenLoader[Q orm.Loadable] struct {
	UserOauthTokens                 func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	PublicUserUser                  func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	CreatorComplianceReportRequests func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	CreatorFiles                    func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	FileuploadPool                  func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	CreatorNoteAudios               func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	DeletorNoteAudios               func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	CreatorNoteImages               func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	DeletorNoteImages               func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	UserNotifications               func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	CreatorPools                    func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	CreatorResidents                func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	CreatorSites                    func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	Organization                    func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
}

func buildUserThenLoader[Q orm.Loadable]() userThenLoader[Q] {
	type UserOauthTokensLoadInterface interface {
		LoadUserOauthTokens(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type PublicUserUserLoadInterface interface {
		LoadPublicUserUser(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type CreatorComplianceReportRequestsLoadInterface interface {
		LoadCreatorComplianceReportRequests(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type CreatorFilesLoadInterface interface {
		LoadCreatorFiles(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type FileuploadPoolLoadInterface interface {
		LoadFileuploadPool(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type CreatorNoteAudiosLoadInterface interface {
		LoadCreatorNoteAudios(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type DeletorNoteAudiosLoadInterface interface {
		LoadDeletorNoteAudios(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type CreatorNoteImagesLoadInterface interface {
		LoadCreatorNoteImages(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type DeletorNoteImagesLoadInterface interface {
		LoadDeletorNoteImages(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type UserNotificationsLoadInterface interface {
		LoadUserNotifications(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type CreatorPoolsLoadInterface interface {
		LoadCreatorPools(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type CreatorResidentsLoadInterface interface {
		LoadCreatorResidents(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type CreatorSitesLoadInterface interface {
		LoadCreatorSites(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type OrganizationLoadInterface interface {
		LoadOrganization(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}

	return userThenLoader[Q]{
		UserOauthTokens: thenLoadBuilder[Q](
			"UserOauthTokens",
			func(ctx context.Context, exec bob.Executor, retrieved UserOauthTokensLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadUserOauthTokens(ctx, exec, mods...)
			},
		),
		PublicUserUser: thenLoadBuilder[Q](
			"PublicUserUser",
			func(ctx context.Context, exec bob.Executor, retrieved PublicUserUserLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadPublicUserUser(ctx, exec, mods...)
			},
		),
		CreatorComplianceReportRequests: thenLoadBuilder[Q](
			"CreatorComplianceReportRequests",
			func(ctx context.Context, exec bob.Executor, retrieved CreatorComplianceReportRequestsLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadCreatorComplianceReportRequests(ctx, exec, mods...)
			},
		),
		CreatorFiles: thenLoadBuilder[Q](
			"CreatorFiles",
			func(ctx context.Context, exec bob.Executor, retrieved CreatorFilesLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadCreatorFiles(ctx, exec, mods...)
			},
		),
		FileuploadPool: thenLoadBuilder[Q](
			"FileuploadPool",
			func(ctx context.Context, exec bob.Executor, retrieved FileuploadPoolLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadFileuploadPool(ctx, exec, mods...)
			},
		),
		CreatorNoteAudios: thenLoadBuilder[Q](
			"CreatorNoteAudios",
			func(ctx context.Context, exec bob.Executor, retrieved CreatorNoteAudiosLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadCreatorNoteAudios(ctx, exec, mods...)
			},
		),
		DeletorNoteAudios: thenLoadBuilder[Q](
			"DeletorNoteAudios",
			func(ctx context.Context, exec bob.Executor, retrieved DeletorNoteAudiosLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadDeletorNoteAudios(ctx, exec, mods...)
			},
		),
		CreatorNoteImages: thenLoadBuilder[Q](
			"CreatorNoteImages",
			func(ctx context.Context, exec bob.Executor, retrieved CreatorNoteImagesLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadCreatorNoteImages(ctx, exec, mods...)
			},
		),
		DeletorNoteImages: thenLoadBuilder[Q](
			"DeletorNoteImages",
			func(ctx context.Context, exec bob.Executor, retrieved DeletorNoteImagesLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadDeletorNoteImages(ctx, exec, mods...)
			},
		),
		UserNotifications: thenLoadBuilder[Q](
			"UserNotifications",
			func(ctx context.Context, exec bob.Executor, retrieved UserNotificationsLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadUserNotifications(ctx, exec, mods...)
			},
		),
		CreatorPools: thenLoadBuilder[Q](
			"CreatorPools",
			func(ctx context.Context, exec bob.Executor, retrieved CreatorPoolsLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadCreatorPools(ctx, exec, mods...)
			},
		),
		CreatorResidents: thenLoadBuilder[Q](
			"CreatorResidents",
			func(ctx context.Context, exec bob.Executor, retrieved CreatorResidentsLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadCreatorResidents(ctx, exec, mods...)
			},
		),
		CreatorSites: thenLoadBuilder[Q](
			"CreatorSites",
			func(ctx context.Context, exec bob.Executor, retrieved CreatorSitesLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadCreatorSites(ctx, exec, mods...)
			},
		),
		Organization: thenLoadBuilder[Q](
			"Organization",
			func(ctx context.Context, exec bob.Executor, retrieved OrganizationLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadOrganization(ctx, exec, mods...)
			},
		),
	}
}

// LoadUserOauthTokens loads the user's UserOauthTokens into the .R struct
func (o *User) LoadUserOauthTokens(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	// Reset the relationship
	o.R.UserOauthTokens = nil

	related, err := o.UserOauthTokens(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, rel := range related {
		rel.R.UserUser = o
	}

	o.R.UserOauthTokens = related
	return nil
}

// LoadUserOauthTokens loads the user's UserOauthTokens into the .R struct
func (os UserSlice) LoadUserOauthTokens(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	arcgisOauthTokens, err := os.UserOauthTokens(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		o.R.UserOauthTokens = nil
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		for _, rel := range arcgisOauthTokens {

			if !(o.ID == rel.UserID) {
				continue
			}

			rel.R.UserUser = o

			o.R.UserOauthTokens = append(o.R.UserOauthTokens, rel)
		}
	}

	return nil
}

// LoadPublicUserUser loads the user's PublicUserUser into the .R struct
func (o *User) LoadPublicUserUser(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	// Reset the relationship
	o.R.PublicUserUser = nil

	related, err := o.PublicUserUser(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, rel := range related {
		rel.R.PublicUserUser = o
	}

	o.R.PublicUserUser = related
	return nil
}

// LoadPublicUserUser loads the user's PublicUserUser into the .R struct
func (os UserSlice) LoadPublicUserUser(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	arcgisusers, err := os.PublicUserUser(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		o.R.PublicUserUser = nil
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		for _, rel := range arcgisusers {

			if !(o.ID == rel.PublicUserID) {
				continue
			}

			rel.R.PublicUserUser = o

			o.R.PublicUserUser = append(o.R.PublicUserUser, rel)
		}
	}

	return nil
}

// LoadCreatorComplianceReportRequests loads the user's CreatorComplianceReportRequests into the .R struct
func (o *User) LoadCreatorComplianceReportRequests(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	// Reset the relationship
	o.R.CreatorComplianceReportRequests = nil

	related, err := o.CreatorComplianceReportRequests(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, rel := range related {
		rel.R.CreatorUser = o
	}

	o.R.CreatorComplianceReportRequests = related
	return nil
}

// LoadCreatorComplianceReportRequests loads the user's CreatorComplianceReportRequests into the .R struct
func (os UserSlice) LoadCreatorComplianceReportRequests(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	complianceReportRequests, err := os.CreatorComplianceReportRequests(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		o.R.CreatorComplianceReportRequests = nil
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		for _, rel := range complianceReportRequests {

			if !(o.ID == rel.Creator) {
				continue
			}

			rel.R.CreatorUser = o

			o.R.CreatorComplianceReportRequests = append(o.R.CreatorComplianceReportRequests, rel)
		}
	}

	return nil
}

// LoadCreatorFiles loads the user's CreatorFiles into the .R struct
func (o *User) LoadCreatorFiles(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	// Reset the relationship
	o.R.CreatorFiles = nil

	related, err := o.CreatorFiles(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, rel := range related {
		rel.R.CreatorUser = o
	}

	o.R.CreatorFiles = related
	return nil
}

// LoadCreatorFiles loads the user's CreatorFiles into the .R struct
func (os UserSlice) LoadCreatorFiles(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	fileuploadFiles, err := os.CreatorFiles(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		o.R.CreatorFiles = nil
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		for _, rel := range fileuploadFiles {

			if !(o.ID == rel.CreatorID) {
				continue
			}

			rel.R.CreatorUser = o

			o.R.CreatorFiles = append(o.R.CreatorFiles, rel)
		}
	}

	return nil
}

// LoadFileuploadPool loads the user's FileuploadPool into the .R struct
func (o *User) LoadFileuploadPool(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	// Reset the relationship
	o.R.FileuploadPool = nil

	related, err := o.FileuploadPool(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, rel := range related {
		rel.R.CreatorUser = o
	}

	o.R.FileuploadPool = related
	return nil
}

// LoadFileuploadPool loads the user's FileuploadPool into the .R struct
func (os UserSlice) LoadFileuploadPool(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	fileuploadPools, err := os.FileuploadPool(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		o.R.FileuploadPool = nil
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		for _, rel := range fileuploadPools {

			if !(o.ID == rel.CreatorID) {
				continue
			}

			rel.R.CreatorUser = o

			o.R.FileuploadPool = append(o.R.FileuploadPool, rel)
		}
	}

	return nil
}

// LoadCreatorNoteAudios loads the user's CreatorNoteAudios into the .R struct
func (o *User) LoadCreatorNoteAudios(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	// Reset the relationship
	o.R.CreatorNoteAudios = nil

	related, err := o.CreatorNoteAudios(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, rel := range related {
		rel.R.CreatorUser = o
	}

	o.R.CreatorNoteAudios = related
	return nil
}

// LoadCreatorNoteAudios loads the user's CreatorNoteAudios into the .R struct
func (os UserSlice) LoadCreatorNoteAudios(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	noteAudios, err := os.CreatorNoteAudios(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		o.R.CreatorNoteAudios = nil
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		for _, rel := range noteAudios {

			if !(o.ID == rel.CreatorID) {
				continue
			}

			rel.R.CreatorUser = o

			o.R.CreatorNoteAudios = append(o.R.CreatorNoteAudios, rel)
		}
	}

	return nil
}

// LoadDeletorNoteAudios loads the user's DeletorNoteAudios into the .R struct
func (o *User) LoadDeletorNoteAudios(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	// Reset the relationship
	o.R.DeletorNoteAudios = nil

	related, err := o.DeletorNoteAudios(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, rel := range related {
		rel.R.DeletorUser = o
	}

	o.R.DeletorNoteAudios = related
	return nil
}

// LoadDeletorNoteAudios loads the user's DeletorNoteAudios into the .R struct
func (os UserSlice) LoadDeletorNoteAudios(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	noteAudios, err := os.DeletorNoteAudios(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		o.R.DeletorNoteAudios = nil
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		for _, rel := range noteAudios {

			if !rel.DeletorID.IsValue() {
				continue
			}
			if !(rel.DeletorID.IsValue() && o.ID == rel.DeletorID.MustGet()) {
				continue
			}

			rel.R.DeletorUser = o

			o.R.DeletorNoteAudios = append(o.R.DeletorNoteAudios, rel)
		}
	}

	return nil
}

// LoadCreatorNoteImages loads the user's CreatorNoteImages into the .R struct
func (o *User) LoadCreatorNoteImages(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	// Reset the relationship
	o.R.CreatorNoteImages = nil

	related, err := o.CreatorNoteImages(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, rel := range related {
		rel.R.CreatorUser = o
	}

	o.R.CreatorNoteImages = related
	return nil
}

// LoadCreatorNoteImages loads the user's CreatorNoteImages into the .R struct
func (os UserSlice) LoadCreatorNoteImages(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	noteImages, err := os.CreatorNoteImages(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		o.R.CreatorNoteImages = nil
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		for _, rel := range noteImages {

			if !(o.ID == rel.CreatorID) {
				continue
			}

			rel.R.CreatorUser = o

			o.R.CreatorNoteImages = append(o.R.CreatorNoteImages, rel)
		}
	}

	return nil
}

// LoadDeletorNoteImages loads the user's DeletorNoteImages into the .R struct
func (o *User) LoadDeletorNoteImages(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	// Reset the relationship
	o.R.DeletorNoteImages = nil

	related, err := o.DeletorNoteImages(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, rel := range related {
		rel.R.DeletorUser = o
	}

	o.R.DeletorNoteImages = related
	return nil
}

// LoadDeletorNoteImages loads the user's DeletorNoteImages into the .R struct
func (os UserSlice) LoadDeletorNoteImages(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	noteImages, err := os.DeletorNoteImages(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		o.R.DeletorNoteImages = nil
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		for _, rel := range noteImages {

			if !rel.DeletorID.IsValue() {
				continue
			}
			if !(rel.DeletorID.IsValue() && o.ID == rel.DeletorID.MustGet()) {
				continue
			}

			rel.R.DeletorUser = o

			o.R.DeletorNoteImages = append(o.R.DeletorNoteImages, rel)
		}
	}

	return nil
}

// LoadUserNotifications loads the user's UserNotifications into the .R struct
func (o *User) LoadUserNotifications(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	// Reset the relationship
	o.R.UserNotifications = nil

	related, err := o.UserNotifications(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, rel := range related {
		rel.R.UserUser = o
	}

	o.R.UserNotifications = related
	return nil
}

// LoadUserNotifications loads the user's UserNotifications into the .R struct
func (os UserSlice) LoadUserNotifications(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	notifications, err := os.UserNotifications(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		o.R.UserNotifications = nil
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		for _, rel := range notifications {

			if !(o.ID == rel.UserID) {
				continue
			}

			rel.R.UserUser = o

			o.R.UserNotifications = append(o.R.UserNotifications, rel)
		}
	}

	return nil
}

// LoadCreatorPools loads the user's CreatorPools into the .R struct
func (o *User) LoadCreatorPools(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	// Reset the relationship
	o.R.CreatorPools = nil

	related, err := o.CreatorPools(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, rel := range related {
		rel.R.CreatorUser = o
	}

	o.R.CreatorPools = related
	return nil
}

// LoadCreatorPools loads the user's CreatorPools into the .R struct
func (os UserSlice) LoadCreatorPools(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	pools, err := os.CreatorPools(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		o.R.CreatorPools = nil
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		for _, rel := range pools {

			if !(o.ID == rel.CreatorID) {
				continue
			}

			rel.R.CreatorUser = o

			o.R.CreatorPools = append(o.R.CreatorPools, rel)
		}
	}

	return nil
}

// LoadCreatorResidents loads the user's CreatorResidents into the .R struct
func (o *User) LoadCreatorResidents(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	// Reset the relationship
	o.R.CreatorResidents = nil

	related, err := o.CreatorResidents(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, rel := range related {
		rel.R.CreatorUser = o
	}

	o.R.CreatorResidents = related
	return nil
}

// LoadCreatorResidents loads the user's CreatorResidents into the .R struct
func (os UserSlice) LoadCreatorResidents(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	residents, err := os.CreatorResidents(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		o.R.CreatorResidents = nil
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		for _, rel := range residents {

			if !(o.ID == rel.Creator) {
				continue
			}

			rel.R.CreatorUser = o

			o.R.CreatorResidents = append(o.R.CreatorResidents, rel)
		}
	}

	return nil
}

// LoadCreatorSites loads the user's CreatorSites into the .R struct
func (o *User) LoadCreatorSites(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	// Reset the relationship
	o.R.CreatorSites = nil

	related, err := o.CreatorSites(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, rel := range related {
		rel.R.CreatorUser = o
	}

	o.R.CreatorSites = related
	return nil
}

// LoadCreatorSites loads the user's CreatorSites into the .R struct
func (os UserSlice) LoadCreatorSites(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	sites, err := os.CreatorSites(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		o.R.CreatorSites = nil
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		for _, rel := range sites {

			if !(o.ID == rel.CreatorID) {
				continue
			}

			rel.R.CreatorUser = o

			o.R.CreatorSites = append(o.R.CreatorSites, rel)
		}
	}

	return nil
}

// LoadOrganization loads the user's Organization into the .R struct
func (o *User) LoadOrganization(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	// Reset the relationship
	o.R.Organization = nil

	related, err := o.Organization(mods...).One(ctx, exec)
	if err != nil {
		return err
	}

	related.R.User = UserSlice{o}

	o.R.Organization = related
	return nil
}

// LoadOrganization loads the user's Organization into the .R struct
func (os UserSlice) LoadOrganization(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	organizations, err := os.Organization(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		for _, rel := range organizations {

			if !(o.OrganizationID == rel.ID) {
				continue
			}

			rel.R.User = append(rel.R.User, o)

			o.R.Organization = rel
			break
		}
	}

	return nil
}

// userC is where relationship counts are stored.
type userC struct {
	UserOauthTokens                 *int64
	PublicUserUser                  *int64
	CreatorComplianceReportRequests *int64
	CreatorFiles                    *int64
	FileuploadPool                  *int64
	CreatorNoteAudios               *int64
	DeletorNoteAudios               *int64
	CreatorNoteImages               *int64
	DeletorNoteImages               *int64
	UserNotifications               *int64
	CreatorPools                    *int64
	CreatorResidents                *int64
	CreatorSites                    *int64
}

// PreloadCount sets a count in the C struct by name
func (o *User) PreloadCount(name string, count int64) error {
	if o == nil {
		return nil
	}

	switch name {
	case "UserOauthTokens":
		o.C.UserOauthTokens = &count
	case "PublicUserUser":
		o.C.PublicUserUser = &count
	case "CreatorComplianceReportRequests":
		o.C.CreatorComplianceReportRequests = &count
	case "CreatorFiles":
		o.C.CreatorFiles = &count
	case "FileuploadPool":
		o.C.FileuploadPool = &count
	case "CreatorNoteAudios":
		o.C.CreatorNoteAudios = &count
	case "DeletorNoteAudios":
		o.C.DeletorNoteAudios = &count
	case "CreatorNoteImages":
		o.C.CreatorNoteImages = &count
	case "DeletorNoteImages":
		o.C.DeletorNoteImages = &count
	case "UserNotifications":
		o.C.UserNotifications = &count
	case "CreatorPools":
		o.C.CreatorPools = &count
	case "CreatorResidents":
		o.C.CreatorResidents = &count
	case "CreatorSites":
		o.C.CreatorSites = &count
	}
	return nil
}

type userCountPreloader struct {
	UserOauthTokens                 func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader
	PublicUserUser                  func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader
	CreatorComplianceReportRequests func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader
	CreatorFiles                    func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader
	FileuploadPool                  func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader
	CreatorNoteAudios               func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader
	DeletorNoteAudios               func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader
	CreatorNoteImages               func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader
	DeletorNoteImages               func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader
	UserNotifications               func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader
	CreatorPools                    func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader
	CreatorResidents                func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader
	CreatorSites                    func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader
}

func buildUserCountPreloader() userCountPreloader {
	return userCountPreloader{
		UserOauthTokens: func(mods ...bob.Mod[*dialect.SelectQuery]) psql.Preloader {
			return countPreloader[*User]("UserOauthTokens", func(parent string) bob.Expression {
				// Build a correlated subquery: (SELECT COUNT(*) FROM related WHERE fk = parent.pk)
				if parent == "" {
					parent = Users.Alias()
				}

				subqueryMods := []bob.Mod[*dialect.SelectQuery]{
					sm.Columns(psql.Raw("count(*)")),

					sm.From(ArcgisOauthTokens.Name()),
					sm.Where(psql.Quote(ArcgisOauthTokens.Alias(), "user_id").EQ(psql.Quote(parent, "id"))),
				}
				subqueryMods = append(subqueryMods, mods...)
				return psql.Group(psql.Select(subqueryMods...).Expression)
			})
		},
		PublicUserUser: func(mods ...bob.Mod[*dialect.SelectQuery]) psql.Preloader {
			return countPreloader[*User]("PublicUserUser", func(parent string) bob.Expression {
				// Build a correlated subquery: (SELECT COUNT(*) FROM related WHERE fk = parent.pk)
				if parent == "" {
					parent = Users.Alias()
				}

				subqueryMods := []bob.Mod[*dialect.SelectQuery]{
					sm.Columns(psql.Raw("count(*)")),

					sm.From(ArcgisUsers.Name()),
					sm.Where(psql.Quote(ArcgisUsers.Alias(), "public_user_id").EQ(psql.Quote(parent, "id"))),
				}
				subqueryMods = append(subqueryMods, mods...)
				return psql.Group(psql.Select(subqueryMods...).Expression)
			})
		},
		CreatorComplianceReportRequests: func(mods ...bob.Mod[*dialect.SelectQuery]) psql.Preloader {
			return countPreloader[*User]("CreatorComplianceReportRequests", func(parent string) bob.Expression {
				// Build a correlated subquery: (SELECT COUNT(*) FROM related WHERE fk = parent.pk)
				if parent == "" {
					parent = Users.Alias()
				}

				subqueryMods := []bob.Mod[*dialect.SelectQuery]{
					sm.Columns(psql.Raw("count(*)")),

					sm.From(ComplianceReportRequests.Name()),
					sm.Where(psql.Quote(ComplianceReportRequests.Alias(), "creator").EQ(psql.Quote(parent, "id"))),
				}
				subqueryMods = append(subqueryMods, mods...)
				return psql.Group(psql.Select(subqueryMods...).Expression)
			})
		},
		CreatorFiles: func(mods ...bob.Mod[*dialect.SelectQuery]) psql.Preloader {
			return countPreloader[*User]("CreatorFiles", func(parent string) bob.Expression {
				// Build a correlated subquery: (SELECT COUNT(*) FROM related WHERE fk = parent.pk)
				if parent == "" {
					parent = Users.Alias()
				}

				subqueryMods := []bob.Mod[*dialect.SelectQuery]{
					sm.Columns(psql.Raw("count(*)")),

					sm.From(FileuploadFiles.Name()),
					sm.Where(psql.Quote(FileuploadFiles.Alias(), "creator_id").EQ(psql.Quote(parent, "id"))),
				}
				subqueryMods = append(subqueryMods, mods...)
				return psql.Group(psql.Select(subqueryMods...).Expression)
			})
		},
		FileuploadPool: func(mods ...bob.Mod[*dialect.SelectQuery]) psql.Preloader {
			return countPreloader[*User]("FileuploadPool", func(parent string) bob.Expression {
				// Build a correlated subquery: (SELECT COUNT(*) FROM related WHERE fk = parent.pk)
				if parent == "" {
					parent = Users.Alias()
				}

				subqueryMods := []bob.Mod[*dialect.SelectQuery]{
					sm.Columns(psql.Raw("count(*)")),

					sm.From(FileuploadPools.Name()),
					sm.Where(psql.Quote(FileuploadPools.Alias(), "creator_id").EQ(psql.Quote(parent, "id"))),
				}
				subqueryMods = append(subqueryMods, mods...)
				return psql.Group(psql.Select(subqueryMods...).Expression)
			})
		},
		CreatorNoteAudios: func(mods ...bob.Mod[*dialect.SelectQuery]) psql.Preloader {
			return countPreloader[*User]("CreatorNoteAudios", func(parent string) bob.Expression {
				// Build a correlated subquery: (SELECT COUNT(*) FROM related WHERE fk = parent.pk)
				if parent == "" {
					parent = Users.Alias()
				}

				subqueryMods := []bob.Mod[*dialect.SelectQuery]{
					sm.Columns(psql.Raw("count(*)")),

					sm.From(NoteAudios.Name()),
					sm.Where(psql.Quote(NoteAudios.Alias(), "creator_id").EQ(psql.Quote(parent, "id"))),
				}
				subqueryMods = append(subqueryMods, mods...)
				return psql.Group(psql.Select(subqueryMods...).Expression)
			})
		},
		DeletorNoteAudios: func(mods ...bob.Mod[*dialect.SelectQuery]) psql.Preloader {
			return countPreloader[*User]("DeletorNoteAudios", func(parent string) bob.Expression {
				// Build a correlated subquery: (SELECT COUNT(*) FROM related WHERE fk = parent.pk)
				if parent == "" {
					parent = Users.Alias()
				}

				subqueryMods := []bob.Mod[*dialect.SelectQuery]{
					sm.Columns(psql.Raw("count(*)")),

					sm.From(NoteAudios.Name()),
					sm.Where(psql.Quote(NoteAudios.Alias(), "deletor_id").EQ(psql.Quote(parent, "id"))),
				}
				subqueryMods = append(subqueryMods, mods...)
				return psql.Group(psql.Select(subqueryMods...).Expression)
			})
		},
		CreatorNoteImages: func(mods ...bob.Mod[*dialect.SelectQuery]) psql.Preloader {
			return countPreloader[*User]("CreatorNoteImages", func(parent string) bob.Expression {
				// Build a correlated subquery: (SELECT COUNT(*) FROM related WHERE fk = parent.pk)
				if parent == "" {
					parent = Users.Alias()
				}

				subqueryMods := []bob.Mod[*dialect.SelectQuery]{
					sm.Columns(psql.Raw("count(*)")),

					sm.From(NoteImages.Name()),
					sm.Where(psql.Quote(NoteImages.Alias(), "creator_id").EQ(psql.Quote(parent, "id"))),
				}
				subqueryMods = append(subqueryMods, mods...)
				return psql.Group(psql.Select(subqueryMods...).Expression)
			})
		},
		DeletorNoteImages: func(mods ...bob.Mod[*dialect.SelectQuery]) psql.Preloader {
			return countPreloader[*User]("DeletorNoteImages", func(parent string) bob.Expression {
				// Build a correlated subquery: (SELECT COUNT(*) FROM related WHERE fk = parent.pk)
				if parent == "" {
					parent = Users.Alias()
				}

				subqueryMods := []bob.Mod[*dialect.SelectQuery]{
					sm.Columns(psql.Raw("count(*)")),

					sm.From(NoteImages.Name()),
					sm.Where(psql.Quote(NoteImages.Alias(), "deletor_id").EQ(psql.Quote(parent, "id"))),
				}
				subqueryMods = append(subqueryMods, mods...)
				return psql.Group(psql.Select(subqueryMods...).Expression)
			})
		},
		UserNotifications: func(mods ...bob.Mod[*dialect.SelectQuery]) psql.Preloader {
			return countPreloader[*User]("UserNotifications", func(parent string) bob.Expression {
				// Build a correlated subquery: (SELECT COUNT(*) FROM related WHERE fk = parent.pk)
				if parent == "" {
					parent = Users.Alias()
				}

				subqueryMods := []bob.Mod[*dialect.SelectQuery]{
					sm.Columns(psql.Raw("count(*)")),

					sm.From(Notifications.Name()),
					sm.Where(psql.Quote(Notifications.Alias(), "user_id").EQ(psql.Quote(parent, "id"))),
				}
				subqueryMods = append(subqueryMods, mods...)
				return psql.Group(psql.Select(subqueryMods...).Expression)
			})
		},
		CreatorPools: func(mods ...bob.Mod[*dialect.SelectQuery]) psql.Preloader {
			return countPreloader[*User]("CreatorPools", func(parent string) bob.Expression {
				// Build a correlated subquery: (SELECT COUNT(*) FROM related WHERE fk = parent.pk)
				if parent == "" {
					parent = Users.Alias()
				}

				subqueryMods := []bob.Mod[*dialect.SelectQuery]{
					sm.Columns(psql.Raw("count(*)")),

					sm.From(Pools.Name()),
					sm.Where(psql.Quote(Pools.Alias(), "creator_id").EQ(psql.Quote(parent, "id"))),
				}
				subqueryMods = append(subqueryMods, mods...)
				return psql.Group(psql.Select(subqueryMods...).Expression)
			})
		},
		CreatorResidents: func(mods ...bob.Mod[*dialect.SelectQuery]) psql.Preloader {
			return countPreloader[*User]("CreatorResidents", func(parent string) bob.Expression {
				// Build a correlated subquery: (SELECT COUNT(*) FROM related WHERE fk = parent.pk)
				if parent == "" {
					parent = Users.Alias()
				}

				subqueryMods := []bob.Mod[*dialect.SelectQuery]{
					sm.Columns(psql.Raw("count(*)")),

					sm.From(Residents.Name()),
					sm.Where(psql.Quote(Residents.Alias(), "creator").EQ(psql.Quote(parent, "id"))),
				}
				subqueryMods = append(subqueryMods, mods...)
				return psql.Group(psql.Select(subqueryMods...).Expression)
			})
		},
		CreatorSites: func(mods ...bob.Mod[*dialect.SelectQuery]) psql.Preloader {
			return countPreloader[*User]("CreatorSites", func(parent string) bob.Expression {
				// Build a correlated subquery: (SELECT COUNT(*) FROM related WHERE fk = parent.pk)
				if parent == "" {
					parent = Users.Alias()
				}

				subqueryMods := []bob.Mod[*dialect.SelectQuery]{
					sm.Columns(psql.Raw("count(*)")),

					sm.From(Sites.Name()),
					sm.Where(psql.Quote(Sites.Alias(), "creator_id").EQ(psql.Quote(parent, "id"))),
				}
				subqueryMods = append(subqueryMods, mods...)
				return psql.Group(psql.Select(subqueryMods...).Expression)
			})
		},
	}
}

type userCountThenLoader[Q orm.Loadable] struct {
	UserOauthTokens                 func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	PublicUserUser                  func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	CreatorComplianceReportRequests func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	CreatorFiles                    func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	FileuploadPool                  func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	CreatorNoteAudios               func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	DeletorNoteAudios               func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	CreatorNoteImages               func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	DeletorNoteImages               func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	UserNotifications               func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	CreatorPools                    func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	CreatorResidents                func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	CreatorSites                    func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
}

func buildUserCountThenLoader[Q orm.Loadable]() userCountThenLoader[Q] {
	type UserOauthTokensCountInterface interface {
		LoadCountUserOauthTokens(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type PublicUserUserCountInterface interface {
		LoadCountPublicUserUser(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type CreatorComplianceReportRequestsCountInterface interface {
		LoadCountCreatorComplianceReportRequests(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type CreatorFilesCountInterface interface {
		LoadCountCreatorFiles(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type FileuploadPoolCountInterface interface {
		LoadCountFileuploadPool(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type CreatorNoteAudiosCountInterface interface {
		LoadCountCreatorNoteAudios(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type DeletorNoteAudiosCountInterface interface {
		LoadCountDeletorNoteAudios(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type CreatorNoteImagesCountInterface interface {
		LoadCountCreatorNoteImages(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type DeletorNoteImagesCountInterface interface {
		LoadCountDeletorNoteImages(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type UserNotificationsCountInterface interface {
		LoadCountUserNotifications(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type CreatorPoolsCountInterface interface {
		LoadCountCreatorPools(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type CreatorResidentsCountInterface interface {
		LoadCountCreatorResidents(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type CreatorSitesCountInterface interface {
		LoadCountCreatorSites(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}

	return userCountThenLoader[Q]{
		UserOauthTokens: countThenLoadBuilder[Q](
			"UserOauthTokens",
			func(ctx context.Context, exec bob.Executor, retrieved UserOauthTokensCountInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadCountUserOauthTokens(ctx, exec, mods...)
			},
		),
		PublicUserUser: countThenLoadBuilder[Q](
			"PublicUserUser",
			func(ctx context.Context, exec bob.Executor, retrieved PublicUserUserCountInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadCountPublicUserUser(ctx, exec, mods...)
			},
		),
		CreatorComplianceReportRequests: countThenLoadBuilder[Q](
			"CreatorComplianceReportRequests",
			func(ctx context.Context, exec bob.Executor, retrieved CreatorComplianceReportRequestsCountInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadCountCreatorComplianceReportRequests(ctx, exec, mods...)
			},
		),
		CreatorFiles: countThenLoadBuilder[Q](
			"CreatorFiles",
			func(ctx context.Context, exec bob.Executor, retrieved CreatorFilesCountInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadCountCreatorFiles(ctx, exec, mods...)
			},
		),
		FileuploadPool: countThenLoadBuilder[Q](
			"FileuploadPool",
			func(ctx context.Context, exec bob.Executor, retrieved FileuploadPoolCountInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadCountFileuploadPool(ctx, exec, mods...)
			},
		),
		CreatorNoteAudios: countThenLoadBuilder[Q](
			"CreatorNoteAudios",
			func(ctx context.Context, exec bob.Executor, retrieved CreatorNoteAudiosCountInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadCountCreatorNoteAudios(ctx, exec, mods...)
			},
		),
		DeletorNoteAudios: countThenLoadBuilder[Q](
			"DeletorNoteAudios",
			func(ctx context.Context, exec bob.Executor, retrieved DeletorNoteAudiosCountInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadCountDeletorNoteAudios(ctx, exec, mods...)
			},
		),
		CreatorNoteImages: countThenLoadBuilder[Q](
			"CreatorNoteImages",
			func(ctx context.Context, exec bob.Executor, retrieved CreatorNoteImagesCountInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadCountCreatorNoteImages(ctx, exec, mods...)
			},
		),
		DeletorNoteImages: countThenLoadBuilder[Q](
			"DeletorNoteImages",
			func(ctx context.Context, exec bob.Executor, retrieved DeletorNoteImagesCountInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadCountDeletorNoteImages(ctx, exec, mods...)
			},
		),
		UserNotifications: countThenLoadBuilder[Q](
			"UserNotifications",
			func(ctx context.Context, exec bob.Executor, retrieved UserNotificationsCountInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadCountUserNotifications(ctx, exec, mods...)
			},
		),
		CreatorPools: countThenLoadBuilder[Q](
			"CreatorPools",
			func(ctx context.Context, exec bob.Executor, retrieved CreatorPoolsCountInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadCountCreatorPools(ctx, exec, mods...)
			},
		),
		CreatorResidents: countThenLoadBuilder[Q](
			"CreatorResidents",
			func(ctx context.Context, exec bob.Executor, retrieved CreatorResidentsCountInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadCountCreatorResidents(ctx, exec, mods...)
			},
		),
		CreatorSites: countThenLoadBuilder[Q](
			"CreatorSites",
			func(ctx context.Context, exec bob.Executor, retrieved CreatorSitesCountInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadCountCreatorSites(ctx, exec, mods...)
			},
		),
	}
}

// LoadCountUserOauthTokens loads the count of UserOauthTokens into the C struct
func (o *User) LoadCountUserOauthTokens(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	count, err := o.UserOauthTokens(mods...).Count(ctx, exec)
	if err != nil {
		return err
	}

	o.C.UserOauthTokens = &count
	return nil
}

// LoadCountUserOauthTokens loads the count of UserOauthTokens for a slice
func (os UserSlice) LoadCountUserOauthTokens(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	for _, o := range os {
		if err := o.LoadCountUserOauthTokens(ctx, exec, mods...); err != nil {
			return err
		}
	}

	return nil
}

// LoadCountPublicUserUser loads the count of PublicUserUser into the C struct
func (o *User) LoadCountPublicUserUser(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	count, err := o.PublicUserUser(mods...).Count(ctx, exec)
	if err != nil {
		return err
	}

	o.C.PublicUserUser = &count
	return nil
}

// LoadCountPublicUserUser loads the count of PublicUserUser for a slice
func (os UserSlice) LoadCountPublicUserUser(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	for _, o := range os {
		if err := o.LoadCountPublicUserUser(ctx, exec, mods...); err != nil {
			return err
		}
	}

	return nil
}

// LoadCountCreatorComplianceReportRequests loads the count of CreatorComplianceReportRequests into the C struct
func (o *User) LoadCountCreatorComplianceReportRequests(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	count, err := o.CreatorComplianceReportRequests(mods...).Count(ctx, exec)
	if err != nil {
		return err
	}

	o.C.CreatorComplianceReportRequests = &count
	return nil
}

// LoadCountCreatorComplianceReportRequests loads the count of CreatorComplianceReportRequests for a slice
func (os UserSlice) LoadCountCreatorComplianceReportRequests(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	for _, o := range os {
		if err := o.LoadCountCreatorComplianceReportRequests(ctx, exec, mods...); err != nil {
			return err
		}
	}

	return nil
}

// LoadCountCreatorFiles loads the count of CreatorFiles into the C struct
func (o *User) LoadCountCreatorFiles(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	count, err := o.CreatorFiles(mods...).Count(ctx, exec)
	if err != nil {
		return err
	}

	o.C.CreatorFiles = &count
	return nil
}

// LoadCountCreatorFiles loads the count of CreatorFiles for a slice
func (os UserSlice) LoadCountCreatorFiles(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	for _, o := range os {
		if err := o.LoadCountCreatorFiles(ctx, exec, mods...); err != nil {
			return err
		}
	}

	return nil
}

// LoadCountFileuploadPool loads the count of FileuploadPool into the C struct
func (o *User) LoadCountFileuploadPool(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	count, err := o.FileuploadPool(mods...).Count(ctx, exec)
	if err != nil {
		return err
	}

	o.C.FileuploadPool = &count
	return nil
}

// LoadCountFileuploadPool loads the count of FileuploadPool for a slice
func (os UserSlice) LoadCountFileuploadPool(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	for _, o := range os {
		if err := o.LoadCountFileuploadPool(ctx, exec, mods...); err != nil {
			return err
		}
	}

	return nil
}

// LoadCountCreatorNoteAudios loads the count of CreatorNoteAudios into the C struct
func (o *User) LoadCountCreatorNoteAudios(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	count, err := o.CreatorNoteAudios(mods...).Count(ctx, exec)
	if err != nil {
		return err
	}

	o.C.CreatorNoteAudios = &count
	return nil
}

// LoadCountCreatorNoteAudios loads the count of CreatorNoteAudios for a slice
func (os UserSlice) LoadCountCreatorNoteAudios(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	for _, o := range os {
		if err := o.LoadCountCreatorNoteAudios(ctx, exec, mods...); err != nil {
			return err
		}
	}

	return nil
}

// LoadCountDeletorNoteAudios loads the count of DeletorNoteAudios into the C struct
func (o *User) LoadCountDeletorNoteAudios(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	count, err := o.DeletorNoteAudios(mods...).Count(ctx, exec)
	if err != nil {
		return err
	}

	o.C.DeletorNoteAudios = &count
	return nil
}

// LoadCountDeletorNoteAudios loads the count of DeletorNoteAudios for a slice
func (os UserSlice) LoadCountDeletorNoteAudios(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	for _, o := range os {
		if err := o.LoadCountDeletorNoteAudios(ctx, exec, mods...); err != nil {
			return err
		}
	}

	return nil
}

// LoadCountCreatorNoteImages loads the count of CreatorNoteImages into the C struct
func (o *User) LoadCountCreatorNoteImages(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	count, err := o.CreatorNoteImages(mods...).Count(ctx, exec)
	if err != nil {
		return err
	}

	o.C.CreatorNoteImages = &count
	return nil
}

// LoadCountCreatorNoteImages loads the count of CreatorNoteImages for a slice
func (os UserSlice) LoadCountCreatorNoteImages(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	for _, o := range os {
		if err := o.LoadCountCreatorNoteImages(ctx, exec, mods...); err != nil {
			return err
		}
	}

	return nil
}

// LoadCountDeletorNoteImages loads the count of DeletorNoteImages into the C struct
func (o *User) LoadCountDeletorNoteImages(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	count, err := o.DeletorNoteImages(mods...).Count(ctx, exec)
	if err != nil {
		return err
	}

	o.C.DeletorNoteImages = &count
	return nil
}

// LoadCountDeletorNoteImages loads the count of DeletorNoteImages for a slice
func (os UserSlice) LoadCountDeletorNoteImages(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	for _, o := range os {
		if err := o.LoadCountDeletorNoteImages(ctx, exec, mods...); err != nil {
			return err
		}
	}

	return nil
}

// LoadCountUserNotifications loads the count of UserNotifications into the C struct
func (o *User) LoadCountUserNotifications(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	count, err := o.UserNotifications(mods...).Count(ctx, exec)
	if err != nil {
		return err
	}

	o.C.UserNotifications = &count
	return nil
}

// LoadCountUserNotifications loads the count of UserNotifications for a slice
func (os UserSlice) LoadCountUserNotifications(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	for _, o := range os {
		if err := o.LoadCountUserNotifications(ctx, exec, mods...); err != nil {
			return err
		}
	}

	return nil
}

// LoadCountCreatorPools loads the count of CreatorPools into the C struct
func (o *User) LoadCountCreatorPools(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	count, err := o.CreatorPools(mods...).Count(ctx, exec)
	if err != nil {
		return err
	}

	o.C.CreatorPools = &count
	return nil
}

// LoadCountCreatorPools loads the count of CreatorPools for a slice
func (os UserSlice) LoadCountCreatorPools(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	for _, o := range os {
		if err := o.LoadCountCreatorPools(ctx, exec, mods...); err != nil {
			return err
		}
	}

	return nil
}

// LoadCountCreatorResidents loads the count of CreatorResidents into the C struct
func (o *User) LoadCountCreatorResidents(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	count, err := o.CreatorResidents(mods...).Count(ctx, exec)
	if err != nil {
		return err
	}

	o.C.CreatorResidents = &count
	return nil
}

// LoadCountCreatorResidents loads the count of CreatorResidents for a slice
func (os UserSlice) LoadCountCreatorResidents(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	for _, o := range os {
		if err := o.LoadCountCreatorResidents(ctx, exec, mods...); err != nil {
			return err
		}
	}

	return nil
}

// LoadCountCreatorSites loads the count of CreatorSites into the C struct
func (o *User) LoadCountCreatorSites(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	count, err := o.CreatorSites(mods...).Count(ctx, exec)
	if err != nil {
		return err
	}

	o.C.CreatorSites = &count
	return nil
}

// LoadCountCreatorSites loads the count of CreatorSites for a slice
func (os UserSlice) LoadCountCreatorSites(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	for _, o := range os {
		if err := o.LoadCountCreatorSites(ctx, exec, mods...); err != nil {
			return err
		}
	}

	return nil
}

type userJoins[Q dialect.Joinable] struct {
	typ                             string
	UserOauthTokens                 modAs[Q, arcgisOauthTokenColumns]
	PublicUserUser                  modAs[Q, arcgisuserColumns]
	CreatorComplianceReportRequests modAs[Q, complianceReportRequestColumns]
	CreatorFiles                    modAs[Q, fileuploadFileColumns]
	FileuploadPool                  modAs[Q, fileuploadPoolColumns]
	CreatorNoteAudios               modAs[Q, noteAudioColumns]
	DeletorNoteAudios               modAs[Q, noteAudioColumns]
	CreatorNoteImages               modAs[Q, noteImageColumns]
	DeletorNoteImages               modAs[Q, noteImageColumns]
	UserNotifications               modAs[Q, notificationColumns]
	CreatorPools                    modAs[Q, poolColumns]
	CreatorResidents                modAs[Q, residentColumns]
	CreatorSites                    modAs[Q, siteColumns]
	Organization                    modAs[Q, organizationColumns]
}

func (j userJoins[Q]) aliasedAs(alias string) userJoins[Q] {
	return buildUserJoins[Q](buildUserColumns(alias), j.typ)
}

func buildUserJoins[Q dialect.Joinable](cols userColumns, typ string) userJoins[Q] {
	return userJoins[Q]{
		typ: typ,
		UserOauthTokens: modAs[Q, arcgisOauthTokenColumns]{
			c: ArcgisOauthTokens.Columns,
			f: func(to arcgisOauthTokenColumns) bob.Mod[Q] {
				mods := make(mods.QueryMods[Q], 0, 1)

				{
					mods = append(mods, dialect.Join[Q](typ, ArcgisOauthTokens.Name().As(to.Alias())).On(
						to.UserID.EQ(cols.ID),
					))
				}

				return mods
			},
		},
		PublicUserUser: modAs[Q, arcgisuserColumns]{
			c: ArcgisUsers.Columns,
			f: func(to arcgisuserColumns) bob.Mod[Q] {
				mods := make(mods.QueryMods[Q], 0, 1)

				{
					mods = append(mods, dialect.Join[Q](typ, ArcgisUsers.Name().As(to.Alias())).On(
						to.PublicUserID.EQ(cols.ID),
					))
				}

				return mods
			},
		},
		CreatorComplianceReportRequests: modAs[Q, complianceReportRequestColumns]{
			c: ComplianceReportRequests.Columns,
			f: func(to complianceReportRequestColumns) bob.Mod[Q] {
				mods := make(mods.QueryMods[Q], 0, 1)

				{
					mods = append(mods, dialect.Join[Q](typ, ComplianceReportRequests.Name().As(to.Alias())).On(
						to.Creator.EQ(cols.ID),
					))
				}

				return mods
			},
		},
		CreatorFiles: modAs[Q, fileuploadFileColumns]{
			c: FileuploadFiles.Columns,
			f: func(to fileuploadFileColumns) bob.Mod[Q] {
				mods := make(mods.QueryMods[Q], 0, 1)

				{
					mods = append(mods, dialect.Join[Q](typ, FileuploadFiles.Name().As(to.Alias())).On(
						to.CreatorID.EQ(cols.ID),
					))
				}

				return mods
			},
		},
		FileuploadPool: modAs[Q, fileuploadPoolColumns]{
			c: FileuploadPools.Columns,
			f: func(to fileuploadPoolColumns) bob.Mod[Q] {
				mods := make(mods.QueryMods[Q], 0, 1)

				{
					mods = append(mods, dialect.Join[Q](typ, FileuploadPools.Name().As(to.Alias())).On(
						to.CreatorID.EQ(cols.ID),
					))
				}

				return mods
			},
		},
		CreatorNoteAudios: modAs[Q, noteAudioColumns]{
			c: NoteAudios.Columns,
			f: func(to noteAudioColumns) bob.Mod[Q] {
				mods := make(mods.QueryMods[Q], 0, 1)

				{
					mods = append(mods, dialect.Join[Q](typ, NoteAudios.Name().As(to.Alias())).On(
						to.CreatorID.EQ(cols.ID),
					))
				}

				return mods
			},
		},
		DeletorNoteAudios: modAs[Q, noteAudioColumns]{
			c: NoteAudios.Columns,
			f: func(to noteAudioColumns) bob.Mod[Q] {
				mods := make(mods.QueryMods[Q], 0, 1)

				{
					mods = append(mods, dialect.Join[Q](typ, NoteAudios.Name().As(to.Alias())).On(
						to.DeletorID.EQ(cols.ID),
					))
				}

				return mods
			},
		},
		CreatorNoteImages: modAs[Q, noteImageColumns]{
			c: NoteImages.Columns,
			f: func(to noteImageColumns) bob.Mod[Q] {
				mods := make(mods.QueryMods[Q], 0, 1)

				{
					mods = append(mods, dialect.Join[Q](typ, NoteImages.Name().As(to.Alias())).On(
						to.CreatorID.EQ(cols.ID),
					))
				}

				return mods
			},
		},
		DeletorNoteImages: modAs[Q, noteImageColumns]{
			c: NoteImages.Columns,
			f: func(to noteImageColumns) bob.Mod[Q] {
				mods := make(mods.QueryMods[Q], 0, 1)

				{
					mods = append(mods, dialect.Join[Q](typ, NoteImages.Name().As(to.Alias())).On(
						to.DeletorID.EQ(cols.ID),
					))
				}

				return mods
			},
		},
		UserNotifications: modAs[Q, notificationColumns]{
			c: Notifications.Columns,
			f: func(to notificationColumns) bob.Mod[Q] {
				mods := make(mods.QueryMods[Q], 0, 1)

				{
					mods = append(mods, dialect.Join[Q](typ, Notifications.Name().As(to.Alias())).On(
						to.UserID.EQ(cols.ID),
					))
				}

				return mods
			},
		},
		CreatorPools: modAs[Q, poolColumns]{
			c: Pools.Columns,
			f: func(to poolColumns) bob.Mod[Q] {
				mods := make(mods.QueryMods[Q], 0, 1)

				{
					mods = append(mods, dialect.Join[Q](typ, Pools.Name().As(to.Alias())).On(
						to.CreatorID.EQ(cols.ID),
					))
				}

				return mods
			},
		},
		CreatorResidents: modAs[Q, residentColumns]{
			c: Residents.Columns,
			f: func(to residentColumns) bob.Mod[Q] {
				mods := make(mods.QueryMods[Q], 0, 1)

				{
					mods = append(mods, dialect.Join[Q](typ, Residents.Name().As(to.Alias())).On(
						to.Creator.EQ(cols.ID),
					))
				}

				return mods
			},
		},
		CreatorSites: modAs[Q, siteColumns]{
			c: Sites.Columns,
			f: func(to siteColumns) bob.Mod[Q] {
				mods := make(mods.QueryMods[Q], 0, 1)

				{
					mods = append(mods, dialect.Join[Q](typ, Sites.Name().As(to.Alias())).On(
						to.CreatorID.EQ(cols.ID),
					))
				}

				return mods
			},
		},
		Organization: modAs[Q, organizationColumns]{
			c: Organizations.Columns,
			f: func(to organizationColumns) bob.Mod[Q] {
				mods := make(mods.QueryMods[Q], 0, 1)

				{
					mods = append(mods, dialect.Join[Q](typ, Organizations.Name().As(to.Alias())).On(
						to.ID.EQ(cols.OrganizationID),
					))
				}

				return mods
			},
		},
	}
}
