// 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/orm"
	"github.com/Gleipnir-Technology/bob/types/pgtypes"
	"github.com/aarondl/opt/null"
	"github.com/aarondl/opt/omit"
	"github.com/aarondl/opt/omitnull"
	"github.com/google/uuid"
)

// NoteAudio is an object representing the database table.
type NoteAudio struct {
	Created                 time.Time           `db:"created" `
	CreatorID               int32               `db:"creator_id" `
	Deleted                 null.Val[time.Time] `db:"deleted" `
	DeletorID               null.Val[int32]     `db:"deletor_id" `
	Duration                float32             `db:"duration" `
	OrganizationID          int32               `db:"organization_id" `
	Transcription           null.Val[string]    `db:"transcription" `
	TranscriptionUserEdited bool                `db:"transcription_user_edited" `
	Version                 int32               `db:"version,pk" `
	UUID                    uuid.UUID           `db:"uuid,pk" `
	ID                      int32               `db:"id,generated" `

	R noteAudioR `db:"-" `
}

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

// NoteAudios contains methods to work with the note_audio table
var NoteAudios = psql.NewTablex[*NoteAudio, NoteAudioSlice, *NoteAudioSetter]("", "note_audio", buildNoteAudioColumns("note_audio"))

// NoteAudiosQuery is a query on the note_audio table
type NoteAudiosQuery = *psql.ViewQuery[*NoteAudio, NoteAudioSlice]

// noteAudioR is where relationships are stored.
type noteAudioR struct {
	CreatorUser          *User                    // note_audio.note_audio_creator_id_fkey
	DeletorUser          *User                    // note_audio.note_audio_deletor_id_fkey
	Organization         *Organization            // note_audio.note_audio_organization_id_fkey
	NoteAudioBreadcrumbs NoteAudioBreadcrumbSlice // note_audio_breadcrumb.note_audio_breadcrumb_note_audio_version_note_audio_uuid_fkey
	NoteAudioData        NoteAudioDatumSlice      // note_audio_data.note_audio_data_note_audio_version_note_audio_uuid_fkey
}

func buildNoteAudioColumns(alias string) noteAudioColumns {
	return noteAudioColumns{
		ColumnsExpr: expr.NewColumnsExpr(
			"created", "creator_id", "deleted", "deletor_id", "duration", "organization_id", "transcription", "transcription_user_edited", "version", "uuid", "id",
		).WithParent("note_audio"),
		tableAlias:              alias,
		Created:                 psql.Quote(alias, "created"),
		CreatorID:               psql.Quote(alias, "creator_id"),
		Deleted:                 psql.Quote(alias, "deleted"),
		DeletorID:               psql.Quote(alias, "deletor_id"),
		Duration:                psql.Quote(alias, "duration"),
		OrganizationID:          psql.Quote(alias, "organization_id"),
		Transcription:           psql.Quote(alias, "transcription"),
		TranscriptionUserEdited: psql.Quote(alias, "transcription_user_edited"),
		Version:                 psql.Quote(alias, "version"),
		UUID:                    psql.Quote(alias, "uuid"),
		ID:                      psql.Quote(alias, "id"),
	}
}

type noteAudioColumns struct {
	expr.ColumnsExpr
	tableAlias              string
	Created                 psql.Expression
	CreatorID               psql.Expression
	Deleted                 psql.Expression
	DeletorID               psql.Expression
	Duration                psql.Expression
	OrganizationID          psql.Expression
	Transcription           psql.Expression
	TranscriptionUserEdited psql.Expression
	Version                 psql.Expression
	UUID                    psql.Expression
	ID                      psql.Expression
}

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

func (noteAudioColumns) AliasedAs(alias string) noteAudioColumns {
	return buildNoteAudioColumns(alias)
}

// NoteAudioSetter is used for insert/upsert/update operations
// All values are optional, and do not have to be set
// Generated columns are not included
type NoteAudioSetter struct {
	Created                 omit.Val[time.Time]     `db:"created" `
	CreatorID               omit.Val[int32]         `db:"creator_id" `
	Deleted                 omitnull.Val[time.Time] `db:"deleted" `
	DeletorID               omitnull.Val[int32]     `db:"deletor_id" `
	Duration                omit.Val[float32]       `db:"duration" `
	OrganizationID          omit.Val[int32]         `db:"organization_id" `
	Transcription           omitnull.Val[string]    `db:"transcription" `
	TranscriptionUserEdited omit.Val[bool]          `db:"transcription_user_edited" `
	Version                 omit.Val[int32]         `db:"version,pk" `
	UUID                    omit.Val[uuid.UUID]     `db:"uuid,pk" `
}

func (s NoteAudioSetter) SetColumns() []string {
	vals := make([]string, 0, 10)
	if s.Created.IsValue() {
		vals = append(vals, "created")
	}
	if s.CreatorID.IsValue() {
		vals = append(vals, "creator_id")
	}
	if !s.Deleted.IsUnset() {
		vals = append(vals, "deleted")
	}
	if !s.DeletorID.IsUnset() {
		vals = append(vals, "deletor_id")
	}
	if s.Duration.IsValue() {
		vals = append(vals, "duration")
	}
	if s.OrganizationID.IsValue() {
		vals = append(vals, "organization_id")
	}
	if !s.Transcription.IsUnset() {
		vals = append(vals, "transcription")
	}
	if s.TranscriptionUserEdited.IsValue() {
		vals = append(vals, "transcription_user_edited")
	}
	if s.Version.IsValue() {
		vals = append(vals, "version")
	}
	if s.UUID.IsValue() {
		vals = append(vals, "uuid")
	}
	return vals
}

func (s NoteAudioSetter) Overwrite(t *NoteAudio) {
	if s.Created.IsValue() {
		t.Created = s.Created.MustGet()
	}
	if s.CreatorID.IsValue() {
		t.CreatorID = s.CreatorID.MustGet()
	}
	if !s.Deleted.IsUnset() {
		t.Deleted = s.Deleted.MustGetNull()
	}
	if !s.DeletorID.IsUnset() {
		t.DeletorID = s.DeletorID.MustGetNull()
	}
	if s.Duration.IsValue() {
		t.Duration = s.Duration.MustGet()
	}
	if s.OrganizationID.IsValue() {
		t.OrganizationID = s.OrganizationID.MustGet()
	}
	if !s.Transcription.IsUnset() {
		t.Transcription = s.Transcription.MustGetNull()
	}
	if s.TranscriptionUserEdited.IsValue() {
		t.TranscriptionUserEdited = s.TranscriptionUserEdited.MustGet()
	}
	if s.Version.IsValue() {
		t.Version = s.Version.MustGet()
	}
	if s.UUID.IsValue() {
		t.UUID = s.UUID.MustGet()
	}
}

func (s *NoteAudioSetter) Apply(q *dialect.InsertQuery) {
	q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
		return NoteAudios.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, 10)
		if s.Created.IsValue() {
			vals[0] = psql.Arg(s.Created.MustGet())
		} else {
			vals[0] = psql.Raw("DEFAULT")
		}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	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.Transcription.IsUnset() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			psql.Quote(append(prefix, "transcription")...),
			psql.Arg(s.Transcription),
		}})
	}

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

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

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

	return exprs
}

// FindNoteAudio retrieves a single record by primary key
// If cols is empty Find will return all columns.
func FindNoteAudio(ctx context.Context, exec bob.Executor, VersionPK int32, UUIDPK uuid.UUID, cols ...string) (*NoteAudio, error) {
	if len(cols) == 0 {
		return NoteAudios.Query(
			sm.Where(NoteAudios.Columns.Version.EQ(psql.Arg(VersionPK))),
			sm.Where(NoteAudios.Columns.UUID.EQ(psql.Arg(UUIDPK))),
		).One(ctx, exec)
	}

	return NoteAudios.Query(
		sm.Where(NoteAudios.Columns.Version.EQ(psql.Arg(VersionPK))),
		sm.Where(NoteAudios.Columns.UUID.EQ(psql.Arg(UUIDPK))),
		sm.Columns(NoteAudios.Columns.Only(cols...)),
	).One(ctx, exec)
}

// NoteAudioExists checks the presence of a single record by primary key
func NoteAudioExists(ctx context.Context, exec bob.Executor, VersionPK int32, UUIDPK uuid.UUID) (bool, error) {
	return NoteAudios.Query(
		sm.Where(NoteAudios.Columns.Version.EQ(psql.Arg(VersionPK))),
		sm.Where(NoteAudios.Columns.UUID.EQ(psql.Arg(UUIDPK))),
	).Exists(ctx, exec)
}

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

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

	return err
}

// primaryKeyVals returns the primary key values of the NoteAudio
func (o *NoteAudio) primaryKeyVals() bob.Expression {
	return psql.ArgGroup(
		o.Version,
		o.UUID,
	)
}

func (o *NoteAudio) pkEQ() dialect.Expression {
	return psql.Group(psql.Quote("note_audio", "version"), psql.Quote("note_audio", "uuid")).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 NoteAudio
func (o *NoteAudio) Update(ctx context.Context, exec bob.Executor, s *NoteAudioSetter) error {
	v, err := NoteAudios.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 NoteAudio record with an executor
func (o *NoteAudio) Delete(ctx context.Context, exec bob.Executor) error {
	_, err := NoteAudios.Delete(dm.Where(o.pkEQ())).Exec(ctx, exec)
	return err
}

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

	return nil
}

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

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

	return err
}

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

	return psql.Group(psql.Quote("note_audio", "version"), psql.Quote("note_audio", "uuid")).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 NoteAudioSlice) copyMatchingRows(from ...*NoteAudio) {
	for i, old := range o {
		for _, new := range from {
			if new.Version != old.Version {
				continue
			}
			if new.UUID != old.UUID {
				continue
			}
			new.R = old.R
			o[i] = new
			break
		}
	}
}

// UpdateMod modifies an update query with "WHERE primary_key IN (o...)"
func (o NoteAudioSlice) 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 NoteAudios.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 *NoteAudio:
				o.copyMatchingRows(retrieved)
			case []*NoteAudio:
				o.copyMatchingRows(retrieved...)
			case NoteAudioSlice:
				o.copyMatchingRows(retrieved...)
			default:
				// If the retrieved value is not a NoteAudio or a slice of NoteAudio
				// then run the AfterUpdateHooks on the slice
				_, err = NoteAudios.AfterUpdateHooks.RunHooks(ctx, exec, o)
			}

			return err
		}))

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

// DeleteMod modifies an delete query with "WHERE primary_key IN (o...)"
func (o NoteAudioSlice) 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 NoteAudios.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 *NoteAudio:
				o.copyMatchingRows(retrieved)
			case []*NoteAudio:
				o.copyMatchingRows(retrieved...)
			case NoteAudioSlice:
				o.copyMatchingRows(retrieved...)
			default:
				// If the retrieved value is not a NoteAudio or a slice of NoteAudio
				// then run the AfterDeleteHooks on the slice
				_, err = NoteAudios.AfterDeleteHooks.RunHooks(ctx, exec, o)
			}

			return err
		}))

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

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

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

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

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

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

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

	o.copyMatchingRows(o2...)

	return nil
}

// CreatorUser starts a query for related objects on user_
func (o *NoteAudio) CreatorUser(mods ...bob.Mod[*dialect.SelectQuery]) UsersQuery {
	return Users.Query(append(mods,
		sm.Where(Users.Columns.ID.EQ(psql.Arg(o.CreatorID))),
	)...)
}

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

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

// DeletorUser starts a query for related objects on user_
func (o *NoteAudio) DeletorUser(mods ...bob.Mod[*dialect.SelectQuery]) UsersQuery {
	return Users.Query(append(mods,
		sm.Where(Users.Columns.ID.EQ(psql.Arg(o.DeletorID))),
	)...)
}

func (os NoteAudioSlice) DeletorUser(mods ...bob.Mod[*dialect.SelectQuery]) UsersQuery {
	pkDeletorID := make(pgtypes.Array[null.Val[int32]], 0, len(os))
	for _, o := range os {
		if o == nil {
			continue
		}
		pkDeletorID = append(pkDeletorID, o.DeletorID)
	}
	PKArgExpr := psql.Select(sm.Columns(
		psql.F("unnest", psql.Cast(psql.Arg(pkDeletorID), "integer[]")),
	))

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

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

func (os NoteAudioSlice) 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)),
	)...)
}

// NoteAudioBreadcrumbs starts a query for related objects on note_audio_breadcrumb
func (o *NoteAudio) NoteAudioBreadcrumbs(mods ...bob.Mod[*dialect.SelectQuery]) NoteAudioBreadcrumbsQuery {
	return NoteAudioBreadcrumbs.Query(append(mods,
		sm.Where(NoteAudioBreadcrumbs.Columns.NoteAudioVersion.EQ(psql.Arg(o.Version))), sm.Where(NoteAudioBreadcrumbs.Columns.NoteAudioUUID.EQ(psql.Arg(o.UUID))),
	)...)
}

func (os NoteAudioSlice) NoteAudioBreadcrumbs(mods ...bob.Mod[*dialect.SelectQuery]) NoteAudioBreadcrumbsQuery {
	pkVersion := make(pgtypes.Array[int32], 0, len(os))

	pkUUID := make(pgtypes.Array[uuid.UUID], 0, len(os))
	for _, o := range os {
		if o == nil {
			continue
		}
		pkVersion = append(pkVersion, o.Version)
		pkUUID = append(pkUUID, o.UUID)
	}
	PKArgExpr := psql.Select(sm.Columns(
		psql.F("unnest", psql.Cast(psql.Arg(pkVersion), "integer[]")),
		psql.F("unnest", psql.Cast(psql.Arg(pkUUID), "uuid[]")),
	))

	return NoteAudioBreadcrumbs.Query(append(mods,
		sm.Where(psql.Group(NoteAudioBreadcrumbs.Columns.NoteAudioVersion, NoteAudioBreadcrumbs.Columns.NoteAudioUUID).OP("IN", PKArgExpr)),
	)...)
}

// NoteAudioData starts a query for related objects on note_audio_data
func (o *NoteAudio) NoteAudioData(mods ...bob.Mod[*dialect.SelectQuery]) NoteAudioDataQuery {
	return NoteAudioData.Query(append(mods,
		sm.Where(NoteAudioData.Columns.NoteAudioVersion.EQ(psql.Arg(o.Version))), sm.Where(NoteAudioData.Columns.NoteAudioUUID.EQ(psql.Arg(o.UUID))),
	)...)
}

func (os NoteAudioSlice) NoteAudioData(mods ...bob.Mod[*dialect.SelectQuery]) NoteAudioDataQuery {
	pkVersion := make(pgtypes.Array[int32], 0, len(os))

	pkUUID := make(pgtypes.Array[uuid.UUID], 0, len(os))
	for _, o := range os {
		if o == nil {
			continue
		}
		pkVersion = append(pkVersion, o.Version)
		pkUUID = append(pkUUID, o.UUID)
	}
	PKArgExpr := psql.Select(sm.Columns(
		psql.F("unnest", psql.Cast(psql.Arg(pkVersion), "integer[]")),
		psql.F("unnest", psql.Cast(psql.Arg(pkUUID), "uuid[]")),
	))

	return NoteAudioData.Query(append(mods,
		sm.Where(psql.Group(NoteAudioData.Columns.NoteAudioVersion, NoteAudioData.Columns.NoteAudioUUID).OP("IN", PKArgExpr)),
	)...)
}

func attachNoteAudioCreatorUser0(ctx context.Context, exec bob.Executor, count int, noteAudio0 *NoteAudio, user1 *User) (*NoteAudio, error) {
	setter := &NoteAudioSetter{
		CreatorID: omit.From(user1.ID),
	}

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

	return noteAudio0, nil
}

func (noteAudio0 *NoteAudio) InsertCreatorUser(ctx context.Context, exec bob.Executor, related *UserSetter) error {
	var err error

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

	_, err = attachNoteAudioCreatorUser0(ctx, exec, 1, noteAudio0, user1)
	if err != nil {
		return err
	}

	noteAudio0.R.CreatorUser = user1

	user1.R.CreatorNoteAudios = append(user1.R.CreatorNoteAudios, noteAudio0)

	return nil
}

func (noteAudio0 *NoteAudio) AttachCreatorUser(ctx context.Context, exec bob.Executor, user1 *User) error {
	var err error

	_, err = attachNoteAudioCreatorUser0(ctx, exec, 1, noteAudio0, user1)
	if err != nil {
		return err
	}

	noteAudio0.R.CreatorUser = user1

	user1.R.CreatorNoteAudios = append(user1.R.CreatorNoteAudios, noteAudio0)

	return nil
}

func attachNoteAudioDeletorUser0(ctx context.Context, exec bob.Executor, count int, noteAudio0 *NoteAudio, user1 *User) (*NoteAudio, error) {
	setter := &NoteAudioSetter{
		DeletorID: omitnull.From(user1.ID),
	}

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

	return noteAudio0, nil
}

func (noteAudio0 *NoteAudio) InsertDeletorUser(ctx context.Context, exec bob.Executor, related *UserSetter) error {
	var err error

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

	_, err = attachNoteAudioDeletorUser0(ctx, exec, 1, noteAudio0, user1)
	if err != nil {
		return err
	}

	noteAudio0.R.DeletorUser = user1

	user1.R.DeletorNoteAudios = append(user1.R.DeletorNoteAudios, noteAudio0)

	return nil
}

func (noteAudio0 *NoteAudio) AttachDeletorUser(ctx context.Context, exec bob.Executor, user1 *User) error {
	var err error

	_, err = attachNoteAudioDeletorUser0(ctx, exec, 1, noteAudio0, user1)
	if err != nil {
		return err
	}

	noteAudio0.R.DeletorUser = user1

	user1.R.DeletorNoteAudios = append(user1.R.DeletorNoteAudios, noteAudio0)

	return nil
}

func attachNoteAudioOrganization0(ctx context.Context, exec bob.Executor, count int, noteAudio0 *NoteAudio, organization1 *Organization) (*NoteAudio, error) {
	setter := &NoteAudioSetter{
		OrganizationID: omit.From(organization1.ID),
	}

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

	return noteAudio0, nil
}

func (noteAudio0 *NoteAudio) 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 = attachNoteAudioOrganization0(ctx, exec, 1, noteAudio0, organization1)
	if err != nil {
		return err
	}

	noteAudio0.R.Organization = organization1

	organization1.R.NoteAudios = append(organization1.R.NoteAudios, noteAudio0)

	return nil
}

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

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

	noteAudio0.R.Organization = organization1

	organization1.R.NoteAudios = append(organization1.R.NoteAudios, noteAudio0)

	return nil
}

func insertNoteAudioNoteAudioBreadcrumbs0(ctx context.Context, exec bob.Executor, noteAudioBreadcrumbs1 []*NoteAudioBreadcrumbSetter, noteAudio0 *NoteAudio) (NoteAudioBreadcrumbSlice, error) {
	for i := range noteAudioBreadcrumbs1 {
		noteAudioBreadcrumbs1[i].NoteAudioVersion = omit.From(noteAudio0.Version)
		noteAudioBreadcrumbs1[i].NoteAudioUUID = omit.From(noteAudio0.UUID)
	}

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

	return ret, nil
}

func attachNoteAudioNoteAudioBreadcrumbs0(ctx context.Context, exec bob.Executor, count int, noteAudioBreadcrumbs1 NoteAudioBreadcrumbSlice, noteAudio0 *NoteAudio) (NoteAudioBreadcrumbSlice, error) {
	setter := &NoteAudioBreadcrumbSetter{
		NoteAudioVersion: omit.From(noteAudio0.Version),
		NoteAudioUUID:    omit.From(noteAudio0.UUID),
	}

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

	return noteAudioBreadcrumbs1, nil
}

func (noteAudio0 *NoteAudio) InsertNoteAudioBreadcrumbs(ctx context.Context, exec bob.Executor, related ...*NoteAudioBreadcrumbSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	noteAudioBreadcrumbs1, err := insertNoteAudioNoteAudioBreadcrumbs0(ctx, exec, related, noteAudio0)
	if err != nil {
		return err
	}

	noteAudio0.R.NoteAudioBreadcrumbs = append(noteAudio0.R.NoteAudioBreadcrumbs, noteAudioBreadcrumbs1...)

	for _, rel := range noteAudioBreadcrumbs1 {
		rel.R.NoteAudio = noteAudio0
	}
	return nil
}

func (noteAudio0 *NoteAudio) AttachNoteAudioBreadcrumbs(ctx context.Context, exec bob.Executor, related ...*NoteAudioBreadcrumb) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	noteAudioBreadcrumbs1 := NoteAudioBreadcrumbSlice(related)

	_, err = attachNoteAudioNoteAudioBreadcrumbs0(ctx, exec, len(related), noteAudioBreadcrumbs1, noteAudio0)
	if err != nil {
		return err
	}

	noteAudio0.R.NoteAudioBreadcrumbs = append(noteAudio0.R.NoteAudioBreadcrumbs, noteAudioBreadcrumbs1...)

	for _, rel := range related {
		rel.R.NoteAudio = noteAudio0
	}

	return nil
}

func insertNoteAudioNoteAudioData0(ctx context.Context, exec bob.Executor, noteAudioData1 []*NoteAudioDatumSetter, noteAudio0 *NoteAudio) (NoteAudioDatumSlice, error) {
	for i := range noteAudioData1 {
		noteAudioData1[i].NoteAudioVersion = omit.From(noteAudio0.Version)
		noteAudioData1[i].NoteAudioUUID = omit.From(noteAudio0.UUID)
	}

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

	return ret, nil
}

func attachNoteAudioNoteAudioData0(ctx context.Context, exec bob.Executor, count int, noteAudioData1 NoteAudioDatumSlice, noteAudio0 *NoteAudio) (NoteAudioDatumSlice, error) {
	setter := &NoteAudioDatumSetter{
		NoteAudioVersion: omit.From(noteAudio0.Version),
		NoteAudioUUID:    omit.From(noteAudio0.UUID),
	}

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

	return noteAudioData1, nil
}

func (noteAudio0 *NoteAudio) InsertNoteAudioData(ctx context.Context, exec bob.Executor, related ...*NoteAudioDatumSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	noteAudioData1, err := insertNoteAudioNoteAudioData0(ctx, exec, related, noteAudio0)
	if err != nil {
		return err
	}

	noteAudio0.R.NoteAudioData = append(noteAudio0.R.NoteAudioData, noteAudioData1...)

	for _, rel := range noteAudioData1 {
		rel.R.NoteAudio = noteAudio0
	}
	return nil
}

func (noteAudio0 *NoteAudio) AttachNoteAudioData(ctx context.Context, exec bob.Executor, related ...*NoteAudioDatum) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	noteAudioData1 := NoteAudioDatumSlice(related)

	_, err = attachNoteAudioNoteAudioData0(ctx, exec, len(related), noteAudioData1, noteAudio0)
	if err != nil {
		return err
	}

	noteAudio0.R.NoteAudioData = append(noteAudio0.R.NoteAudioData, noteAudioData1...)

	for _, rel := range related {
		rel.R.NoteAudio = noteAudio0
	}

	return nil
}

type noteAudioWhere[Q psql.Filterable] struct {
	Created                 psql.WhereMod[Q, time.Time]
	CreatorID               psql.WhereMod[Q, int32]
	Deleted                 psql.WhereNullMod[Q, time.Time]
	DeletorID               psql.WhereNullMod[Q, int32]
	Duration                psql.WhereMod[Q, float32]
	OrganizationID          psql.WhereMod[Q, int32]
	Transcription           psql.WhereNullMod[Q, string]
	TranscriptionUserEdited psql.WhereMod[Q, bool]
	Version                 psql.WhereMod[Q, int32]
	UUID                    psql.WhereMod[Q, uuid.UUID]
	ID                      psql.WhereMod[Q, int32]
}

func (noteAudioWhere[Q]) AliasedAs(alias string) noteAudioWhere[Q] {
	return buildNoteAudioWhere[Q](buildNoteAudioColumns(alias))
}

func buildNoteAudioWhere[Q psql.Filterable](cols noteAudioColumns) noteAudioWhere[Q] {
	return noteAudioWhere[Q]{
		Created:                 psql.Where[Q, time.Time](cols.Created),
		CreatorID:               psql.Where[Q, int32](cols.CreatorID),
		Deleted:                 psql.WhereNull[Q, time.Time](cols.Deleted),
		DeletorID:               psql.WhereNull[Q, int32](cols.DeletorID),
		Duration:                psql.Where[Q, float32](cols.Duration),
		OrganizationID:          psql.Where[Q, int32](cols.OrganizationID),
		Transcription:           psql.WhereNull[Q, string](cols.Transcription),
		TranscriptionUserEdited: psql.Where[Q, bool](cols.TranscriptionUserEdited),
		Version:                 psql.Where[Q, int32](cols.Version),
		UUID:                    psql.Where[Q, uuid.UUID](cols.UUID),
		ID:                      psql.Where[Q, int32](cols.ID),
	}
}

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

	switch name {
	case "CreatorUser":
		rel, ok := retrieved.(*User)
		if !ok {
			return fmt.Errorf("noteAudio cannot load %T as %q", retrieved, name)
		}

		o.R.CreatorUser = rel

		if rel != nil {
			rel.R.CreatorNoteAudios = NoteAudioSlice{o}
		}
		return nil
	case "DeletorUser":
		rel, ok := retrieved.(*User)
		if !ok {
			return fmt.Errorf("noteAudio cannot load %T as %q", retrieved, name)
		}

		o.R.DeletorUser = rel

		if rel != nil {
			rel.R.DeletorNoteAudios = NoteAudioSlice{o}
		}
		return nil
	case "Organization":
		rel, ok := retrieved.(*Organization)
		if !ok {
			return fmt.Errorf("noteAudio cannot load %T as %q", retrieved, name)
		}

		o.R.Organization = rel

		if rel != nil {
			rel.R.NoteAudios = NoteAudioSlice{o}
		}
		return nil
	case "NoteAudioBreadcrumbs":
		rels, ok := retrieved.(NoteAudioBreadcrumbSlice)
		if !ok {
			return fmt.Errorf("noteAudio cannot load %T as %q", retrieved, name)
		}

		o.R.NoteAudioBreadcrumbs = rels

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

		o.R.NoteAudioData = rels

		for _, rel := range rels {
			if rel != nil {
				rel.R.NoteAudio = o
			}
		}
		return nil
	default:
		return fmt.Errorf("noteAudio has no relationship %q", name)
	}
}

type noteAudioPreloader struct {
	CreatorUser  func(...psql.PreloadOption) psql.Preloader
	DeletorUser  func(...psql.PreloadOption) psql.Preloader
	Organization func(...psql.PreloadOption) psql.Preloader
}

func buildNoteAudioPreloader() noteAudioPreloader {
	return noteAudioPreloader{
		CreatorUser: func(opts ...psql.PreloadOption) psql.Preloader {
			return psql.Preload[*User, UserSlice](psql.PreloadRel{
				Name: "CreatorUser",
				Sides: []psql.PreloadSide{
					{
						From:        NoteAudios,
						To:          Users,
						FromColumns: []string{"creator_id"},
						ToColumns:   []string{"id"},
					},
				},
			}, Users.Columns.Names(), opts...)
		},
		DeletorUser: func(opts ...psql.PreloadOption) psql.Preloader {
			return psql.Preload[*User, UserSlice](psql.PreloadRel{
				Name: "DeletorUser",
				Sides: []psql.PreloadSide{
					{
						From:        NoteAudios,
						To:          Users,
						FromColumns: []string{"deletor_id"},
						ToColumns:   []string{"id"},
					},
				},
			}, Users.Columns.Names(), opts...)
		},
		Organization: func(opts ...psql.PreloadOption) psql.Preloader {
			return psql.Preload[*Organization, OrganizationSlice](psql.PreloadRel{
				Name: "Organization",
				Sides: []psql.PreloadSide{
					{
						From:        NoteAudios,
						To:          Organizations,
						FromColumns: []string{"organization_id"},
						ToColumns:   []string{"id"},
					},
				},
			}, Organizations.Columns.Names(), opts...)
		},
	}
}

type noteAudioThenLoader[Q orm.Loadable] struct {
	CreatorUser          func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	DeletorUser          func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	Organization         func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	NoteAudioBreadcrumbs func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	NoteAudioData        func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
}

func buildNoteAudioThenLoader[Q orm.Loadable]() noteAudioThenLoader[Q] {
	type CreatorUserLoadInterface interface {
		LoadCreatorUser(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type DeletorUserLoadInterface interface {
		LoadDeletorUser(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type OrganizationLoadInterface interface {
		LoadOrganization(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type NoteAudioBreadcrumbsLoadInterface interface {
		LoadNoteAudioBreadcrumbs(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type NoteAudioDataLoadInterface interface {
		LoadNoteAudioData(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}

	return noteAudioThenLoader[Q]{
		CreatorUser: thenLoadBuilder[Q](
			"CreatorUser",
			func(ctx context.Context, exec bob.Executor, retrieved CreatorUserLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadCreatorUser(ctx, exec, mods...)
			},
		),
		DeletorUser: thenLoadBuilder[Q](
			"DeletorUser",
			func(ctx context.Context, exec bob.Executor, retrieved DeletorUserLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadDeletorUser(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...)
			},
		),
		NoteAudioBreadcrumbs: thenLoadBuilder[Q](
			"NoteAudioBreadcrumbs",
			func(ctx context.Context, exec bob.Executor, retrieved NoteAudioBreadcrumbsLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadNoteAudioBreadcrumbs(ctx, exec, mods...)
			},
		),
		NoteAudioData: thenLoadBuilder[Q](
			"NoteAudioData",
			func(ctx context.Context, exec bob.Executor, retrieved NoteAudioDataLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadNoteAudioData(ctx, exec, mods...)
			},
		),
	}
}

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

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

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

	related.R.CreatorNoteAudios = NoteAudioSlice{o}

	o.R.CreatorUser = related
	return nil
}

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

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

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

		for _, rel := range users {

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

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

			o.R.CreatorUser = rel
			break
		}
	}

	return nil
}

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

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

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

	related.R.DeletorNoteAudios = NoteAudioSlice{o}

	o.R.DeletorUser = related
	return nil
}

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

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

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

		for _, rel := range users {
			if !o.DeletorID.IsValue() {
				continue
			}

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

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

			o.R.DeletorUser = rel
			break
		}
	}

	return nil
}

// LoadOrganization loads the noteAudio's Organization into the .R struct
func (o *NoteAudio) 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.NoteAudios = NoteAudioSlice{o}

	o.R.Organization = related
	return nil
}

// LoadOrganization loads the noteAudio's Organization into the .R struct
func (os NoteAudioSlice) 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.NoteAudios = append(rel.R.NoteAudios, o)

			o.R.Organization = rel
			break
		}
	}

	return nil
}

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

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

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

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

	o.R.NoteAudioBreadcrumbs = related
	return nil
}

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

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

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

		o.R.NoteAudioBreadcrumbs = nil
	}

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

		for _, rel := range noteAudioBreadcrumbs {

			if !(o.Version == rel.NoteAudioVersion) {
				continue
			}

			if !(o.UUID == rel.NoteAudioUUID) {
				continue
			}

			rel.R.NoteAudio = o

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

	return nil
}

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

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

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

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

	o.R.NoteAudioData = related
	return nil
}

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

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

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

		o.R.NoteAudioData = nil
	}

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

		for _, rel := range noteAudioData {

			if !(o.Version == rel.NoteAudioVersion) {
				continue
			}

			if !(o.UUID == rel.NoteAudioUUID) {
				continue
			}

			rel.R.NoteAudio = o

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

	return nil
}
