// 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/omit"
	"github.com/aarondl/opt/omitnull"
)

// Address is an object representing the database table.
type Address struct {
	Country    string    `db:"country" `
	Created    time.Time `db:"created" `
	Location   string    `db:"location" `
	H3cell     string    `db:"h3cell" `
	ID         int32     `db:"id,pk" `
	Locality   string    `db:"locality" `
	PostalCode string    `db:"postal_code" `
	Street     string    `db:"street" `
	Unit       string    `db:"unit" `
	Region     string    `db:"region" `
	Number     string    `db:"number_" `
	Gid        string    `db:"gid" `

	R addressR `db:"-" `
}

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

// Addresses contains methods to work with the address table
var Addresses = psql.NewTablex[*Address, AddressSlice, *AddressSetter]("", "address", buildAddressColumns("address"))

// AddressesQuery is a query on the address table
type AddressesQuery = *psql.ViewQuery[*Address, AddressSlice]

// addressR is where relationships are stored.
type addressR struct {
	Mailers      CommsMailerSlice             // comms.mailer.mailer_address_id_fkey
	Pools        FileuploadPoolSlice          // fileupload.pool.pool_address_id_fkey
	NuisanceOlds PublicreportNuisanceOldSlice // publicreport.nuisance_old.nuisance_address_id_fkey
	Reports      PublicreportReportSlice      // publicreport.report.report_address_id_fkey
	WaterOlds    PublicreportWaterOldSlice    // publicreport.water_old.pool_address_id_fkey
	Residents    ResidentSlice                // resident.resident_address_id_fkey
	Site         *Site                        // site.site_address_id_fkey
}

func buildAddressColumns(alias string) addressColumns {
	return addressColumns{
		ColumnsExpr: expr.NewColumnsExpr(
			"country", "created", "location", "h3cell", "id", "locality", "postal_code", "street", "unit", "region", "number_", "gid",
		).WithParent("address"),
		tableAlias: alias,
		Country:    psql.Quote(alias, "country"),
		Created:    psql.Quote(alias, "created"),
		Location:   psql.Quote(alias, "location"),
		H3cell:     psql.Quote(alias, "h3cell"),
		ID:         psql.Quote(alias, "id"),
		Locality:   psql.Quote(alias, "locality"),
		PostalCode: psql.Quote(alias, "postal_code"),
		Street:     psql.Quote(alias, "street"),
		Unit:       psql.Quote(alias, "unit"),
		Region:     psql.Quote(alias, "region"),
		Number:     psql.Quote(alias, "number_"),
		Gid:        psql.Quote(alias, "gid"),
	}
}

type addressColumns struct {
	expr.ColumnsExpr
	tableAlias string
	Country    psql.Expression
	Created    psql.Expression
	Location   psql.Expression
	H3cell     psql.Expression
	ID         psql.Expression
	Locality   psql.Expression
	PostalCode psql.Expression
	Street     psql.Expression
	Unit       psql.Expression
	Region     psql.Expression
	Number     psql.Expression
	Gid        psql.Expression
}

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

func (addressColumns) AliasedAs(alias string) addressColumns {
	return buildAddressColumns(alias)
}

// AddressSetter is used for insert/upsert/update operations
// All values are optional, and do not have to be set
// Generated columns are not included
type AddressSetter struct {
	Country    omit.Val[string]    `db:"country" `
	Created    omit.Val[time.Time] `db:"created" `
	Location   omit.Val[string]    `db:"location" `
	H3cell     omit.Val[string]    `db:"h3cell" `
	ID         omit.Val[int32]     `db:"id,pk" `
	Locality   omit.Val[string]    `db:"locality" `
	PostalCode omit.Val[string]    `db:"postal_code" `
	Street     omit.Val[string]    `db:"street" `
	Unit       omit.Val[string]    `db:"unit" `
	Region     omit.Val[string]    `db:"region" `
	Number     omit.Val[string]    `db:"number_" `
	Gid        omit.Val[string]    `db:"gid" `
}

func (s AddressSetter) SetColumns() []string {
	vals := make([]string, 0, 12)
	if s.Country.IsValue() {
		vals = append(vals, "country")
	}
	if s.Created.IsValue() {
		vals = append(vals, "created")
	}
	if s.Location.IsValue() {
		vals = append(vals, "location")
	}
	if s.H3cell.IsValue() {
		vals = append(vals, "h3cell")
	}
	if s.ID.IsValue() {
		vals = append(vals, "id")
	}
	if s.Locality.IsValue() {
		vals = append(vals, "locality")
	}
	if s.PostalCode.IsValue() {
		vals = append(vals, "postal_code")
	}
	if s.Street.IsValue() {
		vals = append(vals, "street")
	}
	if s.Unit.IsValue() {
		vals = append(vals, "unit")
	}
	if s.Region.IsValue() {
		vals = append(vals, "region")
	}
	if s.Number.IsValue() {
		vals = append(vals, "number_")
	}
	if s.Gid.IsValue() {
		vals = append(vals, "gid")
	}
	return vals
}

func (s AddressSetter) Overwrite(t *Address) {
	if s.Country.IsValue() {
		t.Country = s.Country.MustGet()
	}
	if s.Created.IsValue() {
		t.Created = s.Created.MustGet()
	}
	if s.Location.IsValue() {
		t.Location = s.Location.MustGet()
	}
	if s.H3cell.IsValue() {
		t.H3cell = s.H3cell.MustGet()
	}
	if s.ID.IsValue() {
		t.ID = s.ID.MustGet()
	}
	if s.Locality.IsValue() {
		t.Locality = s.Locality.MustGet()
	}
	if s.PostalCode.IsValue() {
		t.PostalCode = s.PostalCode.MustGet()
	}
	if s.Street.IsValue() {
		t.Street = s.Street.MustGet()
	}
	if s.Unit.IsValue() {
		t.Unit = s.Unit.MustGet()
	}
	if s.Region.IsValue() {
		t.Region = s.Region.MustGet()
	}
	if s.Number.IsValue() {
		t.Number = s.Number.MustGet()
	}
	if s.Gid.IsValue() {
		t.Gid = s.Gid.MustGet()
	}
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return exprs
}

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

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

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

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

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

	return err
}

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

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

// Reload refreshes the Address using the executor
func (o *Address) Reload(ctx context.Context, exec bob.Executor) error {
	o2, err := Addresses.Query(
		sm.Where(Addresses.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 AddressSlice is retrieved from the database
func (o AddressSlice) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
	var err error

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

	return err
}

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

	return psql.Quote("address", "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 AddressSlice) copyMatchingRows(from ...*Address) {
	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 AddressSlice) 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 Addresses.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 *Address:
				o.copyMatchingRows(retrieved)
			case []*Address:
				o.copyMatchingRows(retrieved...)
			case AddressSlice:
				o.copyMatchingRows(retrieved...)
			default:
				// If the retrieved value is not a Address or a slice of Address
				// then run the AfterUpdateHooks on the slice
				_, err = Addresses.AfterUpdateHooks.RunHooks(ctx, exec, o)
			}

			return err
		}))

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

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

			return err
		}))

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

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

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

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

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

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

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

	o.copyMatchingRows(o2...)

	return nil
}

// Mailers starts a query for related objects on comms.mailer
func (o *Address) Mailers(mods ...bob.Mod[*dialect.SelectQuery]) CommsMailersQuery {
	return CommsMailers.Query(append(mods,
		sm.Where(CommsMailers.Columns.AddressID.EQ(psql.Arg(o.ID))),
	)...)
}

func (os AddressSlice) Mailers(mods ...bob.Mod[*dialect.SelectQuery]) CommsMailersQuery {
	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 CommsMailers.Query(append(mods,
		sm.Where(psql.Group(CommsMailers.Columns.AddressID).OP("IN", PKArgExpr)),
	)...)
}

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

func (os AddressSlice) Pools(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.AddressID).OP("IN", PKArgExpr)),
	)...)
}

// NuisanceOlds starts a query for related objects on publicreport.nuisance_old
func (o *Address) NuisanceOlds(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportNuisanceOldsQuery {
	return PublicreportNuisanceOlds.Query(append(mods,
		sm.Where(PublicreportNuisanceOlds.Columns.AddressID.EQ(psql.Arg(o.ID))),
	)...)
}

func (os AddressSlice) NuisanceOlds(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportNuisanceOldsQuery {
	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 PublicreportNuisanceOlds.Query(append(mods,
		sm.Where(psql.Group(PublicreportNuisanceOlds.Columns.AddressID).OP("IN", PKArgExpr)),
	)...)
}

// Reports starts a query for related objects on publicreport.report
func (o *Address) Reports(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportReportsQuery {
	return PublicreportReports.Query(append(mods,
		sm.Where(PublicreportReports.Columns.AddressID.EQ(psql.Arg(o.ID))),
	)...)
}

func (os AddressSlice) Reports(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportReportsQuery {
	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 PublicreportReports.Query(append(mods,
		sm.Where(psql.Group(PublicreportReports.Columns.AddressID).OP("IN", PKArgExpr)),
	)...)
}

// WaterOlds starts a query for related objects on publicreport.water_old
func (o *Address) WaterOlds(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportWaterOldsQuery {
	return PublicreportWaterOlds.Query(append(mods,
		sm.Where(PublicreportWaterOlds.Columns.AddressID.EQ(psql.Arg(o.ID))),
	)...)
}

func (os AddressSlice) WaterOlds(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportWaterOldsQuery {
	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 PublicreportWaterOlds.Query(append(mods,
		sm.Where(psql.Group(PublicreportWaterOlds.Columns.AddressID).OP("IN", PKArgExpr)),
	)...)
}

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

func (os AddressSlice) Residents(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.AddressID).OP("IN", PKArgExpr)),
	)...)
}

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

func (os AddressSlice) Site(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.AddressID).OP("IN", PKArgExpr)),
	)...)
}

func insertAddressMailers0(ctx context.Context, exec bob.Executor, commsMailers1 []*CommsMailerSetter, address0 *Address) (CommsMailerSlice, error) {
	for i := range commsMailers1 {
		commsMailers1[i].AddressID = omit.From(address0.ID)
	}

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

	return ret, nil
}

func attachAddressMailers0(ctx context.Context, exec bob.Executor, count int, commsMailers1 CommsMailerSlice, address0 *Address) (CommsMailerSlice, error) {
	setter := &CommsMailerSetter{
		AddressID: omit.From(address0.ID),
	}

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

	return commsMailers1, nil
}

func (address0 *Address) InsertMailers(ctx context.Context, exec bob.Executor, related ...*CommsMailerSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	commsMailers1, err := insertAddressMailers0(ctx, exec, related, address0)
	if err != nil {
		return err
	}

	address0.R.Mailers = append(address0.R.Mailers, commsMailers1...)

	for _, rel := range commsMailers1 {
		rel.R.Address = address0
	}
	return nil
}

func (address0 *Address) AttachMailers(ctx context.Context, exec bob.Executor, related ...*CommsMailer) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	commsMailers1 := CommsMailerSlice(related)

	_, err = attachAddressMailers0(ctx, exec, len(related), commsMailers1, address0)
	if err != nil {
		return err
	}

	address0.R.Mailers = append(address0.R.Mailers, commsMailers1...)

	for _, rel := range related {
		rel.R.Address = address0
	}

	return nil
}

func insertAddressPools0(ctx context.Context, exec bob.Executor, fileuploadPools1 []*FileuploadPoolSetter, address0 *Address) (FileuploadPoolSlice, error) {
	for i := range fileuploadPools1 {
		fileuploadPools1[i].AddressID = omitnull.From(address0.ID)
	}

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

	return ret, nil
}

func attachAddressPools0(ctx context.Context, exec bob.Executor, count int, fileuploadPools1 FileuploadPoolSlice, address0 *Address) (FileuploadPoolSlice, error) {
	setter := &FileuploadPoolSetter{
		AddressID: omitnull.From(address0.ID),
	}

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

	return fileuploadPools1, nil
}

func (address0 *Address) InsertPools(ctx context.Context, exec bob.Executor, related ...*FileuploadPoolSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

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

	address0.R.Pools = append(address0.R.Pools, fileuploadPools1...)

	for _, rel := range fileuploadPools1 {
		rel.R.Address = address0
	}
	return nil
}

func (address0 *Address) AttachPools(ctx context.Context, exec bob.Executor, related ...*FileuploadPool) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	fileuploadPools1 := FileuploadPoolSlice(related)

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

	address0.R.Pools = append(address0.R.Pools, fileuploadPools1...)

	for _, rel := range related {
		rel.R.Address = address0
	}

	return nil
}

func insertAddressNuisanceOlds0(ctx context.Context, exec bob.Executor, publicreportNuisanceOlds1 []*PublicreportNuisanceOldSetter, address0 *Address) (PublicreportNuisanceOldSlice, error) {
	for i := range publicreportNuisanceOlds1 {
		publicreportNuisanceOlds1[i].AddressID = omitnull.From(address0.ID)
	}

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

	return ret, nil
}

func attachAddressNuisanceOlds0(ctx context.Context, exec bob.Executor, count int, publicreportNuisanceOlds1 PublicreportNuisanceOldSlice, address0 *Address) (PublicreportNuisanceOldSlice, error) {
	setter := &PublicreportNuisanceOldSetter{
		AddressID: omitnull.From(address0.ID),
	}

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

	return publicreportNuisanceOlds1, nil
}

func (address0 *Address) InsertNuisanceOlds(ctx context.Context, exec bob.Executor, related ...*PublicreportNuisanceOldSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	publicreportNuisanceOlds1, err := insertAddressNuisanceOlds0(ctx, exec, related, address0)
	if err != nil {
		return err
	}

	address0.R.NuisanceOlds = append(address0.R.NuisanceOlds, publicreportNuisanceOlds1...)

	for _, rel := range publicreportNuisanceOlds1 {
		rel.R.Address = address0
	}
	return nil
}

func (address0 *Address) AttachNuisanceOlds(ctx context.Context, exec bob.Executor, related ...*PublicreportNuisanceOld) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	publicreportNuisanceOlds1 := PublicreportNuisanceOldSlice(related)

	_, err = attachAddressNuisanceOlds0(ctx, exec, len(related), publicreportNuisanceOlds1, address0)
	if err != nil {
		return err
	}

	address0.R.NuisanceOlds = append(address0.R.NuisanceOlds, publicreportNuisanceOlds1...)

	for _, rel := range related {
		rel.R.Address = address0
	}

	return nil
}

func insertAddressReports0(ctx context.Context, exec bob.Executor, publicreportReports1 []*PublicreportReportSetter, address0 *Address) (PublicreportReportSlice, error) {
	for i := range publicreportReports1 {
		publicreportReports1[i].AddressID = omitnull.From(address0.ID)
	}

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

	return ret, nil
}

func attachAddressReports0(ctx context.Context, exec bob.Executor, count int, publicreportReports1 PublicreportReportSlice, address0 *Address) (PublicreportReportSlice, error) {
	setter := &PublicreportReportSetter{
		AddressID: omitnull.From(address0.ID),
	}

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

	return publicreportReports1, nil
}

func (address0 *Address) InsertReports(ctx context.Context, exec bob.Executor, related ...*PublicreportReportSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	publicreportReports1, err := insertAddressReports0(ctx, exec, related, address0)
	if err != nil {
		return err
	}

	address0.R.Reports = append(address0.R.Reports, publicreportReports1...)

	for _, rel := range publicreportReports1 {
		rel.R.Address = address0
	}
	return nil
}

func (address0 *Address) AttachReports(ctx context.Context, exec bob.Executor, related ...*PublicreportReport) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	publicreportReports1 := PublicreportReportSlice(related)

	_, err = attachAddressReports0(ctx, exec, len(related), publicreportReports1, address0)
	if err != nil {
		return err
	}

	address0.R.Reports = append(address0.R.Reports, publicreportReports1...)

	for _, rel := range related {
		rel.R.Address = address0
	}

	return nil
}

func insertAddressWaterOlds0(ctx context.Context, exec bob.Executor, publicreportWaterOlds1 []*PublicreportWaterOldSetter, address0 *Address) (PublicreportWaterOldSlice, error) {
	for i := range publicreportWaterOlds1 {
		publicreportWaterOlds1[i].AddressID = omitnull.From(address0.ID)
	}

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

	return ret, nil
}

func attachAddressWaterOlds0(ctx context.Context, exec bob.Executor, count int, publicreportWaterOlds1 PublicreportWaterOldSlice, address0 *Address) (PublicreportWaterOldSlice, error) {
	setter := &PublicreportWaterOldSetter{
		AddressID: omitnull.From(address0.ID),
	}

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

	return publicreportWaterOlds1, nil
}

func (address0 *Address) InsertWaterOlds(ctx context.Context, exec bob.Executor, related ...*PublicreportWaterOldSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	publicreportWaterOlds1, err := insertAddressWaterOlds0(ctx, exec, related, address0)
	if err != nil {
		return err
	}

	address0.R.WaterOlds = append(address0.R.WaterOlds, publicreportWaterOlds1...)

	for _, rel := range publicreportWaterOlds1 {
		rel.R.Address = address0
	}
	return nil
}

func (address0 *Address) AttachWaterOlds(ctx context.Context, exec bob.Executor, related ...*PublicreportWaterOld) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	publicreportWaterOlds1 := PublicreportWaterOldSlice(related)

	_, err = attachAddressWaterOlds0(ctx, exec, len(related), publicreportWaterOlds1, address0)
	if err != nil {
		return err
	}

	address0.R.WaterOlds = append(address0.R.WaterOlds, publicreportWaterOlds1...)

	for _, rel := range related {
		rel.R.Address = address0
	}

	return nil
}

func insertAddressResidents0(ctx context.Context, exec bob.Executor, residents1 []*ResidentSetter, address0 *Address) (ResidentSlice, error) {
	for i := range residents1 {
		residents1[i].AddressID = omit.From(address0.ID)
	}

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

	return ret, nil
}

func attachAddressResidents0(ctx context.Context, exec bob.Executor, count int, residents1 ResidentSlice, address0 *Address) (ResidentSlice, error) {
	setter := &ResidentSetter{
		AddressID: omit.From(address0.ID),
	}

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

	return residents1, nil
}

func (address0 *Address) InsertResidents(ctx context.Context, exec bob.Executor, related ...*ResidentSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

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

	address0.R.Residents = append(address0.R.Residents, residents1...)

	for _, rel := range residents1 {
		rel.R.Address = address0
	}
	return nil
}

func (address0 *Address) AttachResidents(ctx context.Context, exec bob.Executor, related ...*Resident) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	residents1 := ResidentSlice(related)

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

	address0.R.Residents = append(address0.R.Residents, residents1...)

	for _, rel := range related {
		rel.R.Address = address0
	}

	return nil
}

func insertAddressSite0(ctx context.Context, exec bob.Executor, site1 *SiteSetter, address0 *Address) (*Site, error) {
	site1.AddressID = omit.From(address0.ID)

	ret, err := Sites.Insert(site1).One(ctx, exec)
	if err != nil {
		return ret, fmt.Errorf("insertAddressSite0: %w", err)
	}

	return ret, nil
}

func attachAddressSite0(ctx context.Context, exec bob.Executor, count int, site1 *Site, address0 *Address) (*Site, error) {
	setter := &SiteSetter{
		AddressID: omit.From(address0.ID),
	}

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

	return site1, nil
}

func (address0 *Address) InsertSite(ctx context.Context, exec bob.Executor, related *SiteSetter) error {
	var err error

	site1, err := insertAddressSite0(ctx, exec, related, address0)
	if err != nil {
		return err
	}

	address0.R.Site = site1

	site1.R.Address = address0

	return nil
}

func (address0 *Address) AttachSite(ctx context.Context, exec bob.Executor, site1 *Site) error {
	var err error

	_, err = attachAddressSite0(ctx, exec, 1, site1, address0)
	if err != nil {
		return err
	}

	address0.R.Site = site1

	site1.R.Address = address0

	return nil
}

type addressWhere[Q psql.Filterable] struct {
	Country    psql.WhereMod[Q, string]
	Created    psql.WhereMod[Q, time.Time]
	Location   psql.WhereMod[Q, string]
	H3cell     psql.WhereMod[Q, string]
	ID         psql.WhereMod[Q, int32]
	Locality   psql.WhereMod[Q, string]
	PostalCode psql.WhereMod[Q, string]
	Street     psql.WhereMod[Q, string]
	Unit       psql.WhereMod[Q, string]
	Region     psql.WhereMod[Q, string]
	Number     psql.WhereMod[Q, string]
	Gid        psql.WhereMod[Q, string]
}

func (addressWhere[Q]) AliasedAs(alias string) addressWhere[Q] {
	return buildAddressWhere[Q](buildAddressColumns(alias))
}

func buildAddressWhere[Q psql.Filterable](cols addressColumns) addressWhere[Q] {
	return addressWhere[Q]{
		Country:    psql.Where[Q, string](cols.Country),
		Created:    psql.Where[Q, time.Time](cols.Created),
		Location:   psql.Where[Q, string](cols.Location),
		H3cell:     psql.Where[Q, string](cols.H3cell),
		ID:         psql.Where[Q, int32](cols.ID),
		Locality:   psql.Where[Q, string](cols.Locality),
		PostalCode: psql.Where[Q, string](cols.PostalCode),
		Street:     psql.Where[Q, string](cols.Street),
		Unit:       psql.Where[Q, string](cols.Unit),
		Region:     psql.Where[Q, string](cols.Region),
		Number:     psql.Where[Q, string](cols.Number),
		Gid:        psql.Where[Q, string](cols.Gid),
	}
}

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

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

		o.R.Mailers = rels

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

		o.R.Pools = rels

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

		o.R.NuisanceOlds = rels

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

		o.R.Reports = rels

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

		o.R.WaterOlds = rels

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

		o.R.Residents = rels

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

		o.R.Site = rel

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

type addressPreloader struct {
	Site func(...psql.PreloadOption) psql.Preloader
}

func buildAddressPreloader() addressPreloader {
	return addressPreloader{
		Site: func(opts ...psql.PreloadOption) psql.Preloader {
			return psql.Preload[*Site, SiteSlice](psql.PreloadRel{
				Name: "Site",
				Sides: []psql.PreloadSide{
					{
						From:        Addresses,
						To:          Sites,
						FromColumns: []string{"id"},
						ToColumns:   []string{"address_id"},
					},
				},
			}, Sites.Columns.Names(), opts...)
		},
	}
}

type addressThenLoader[Q orm.Loadable] struct {
	Mailers      func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	Pools        func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	NuisanceOlds func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	Reports      func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	WaterOlds    func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	Residents    func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	Site         func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
}

func buildAddressThenLoader[Q orm.Loadable]() addressThenLoader[Q] {
	type MailersLoadInterface interface {
		LoadMailers(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type PoolsLoadInterface interface {
		LoadPools(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type NuisanceOldsLoadInterface interface {
		LoadNuisanceOlds(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type ReportsLoadInterface interface {
		LoadReports(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type WaterOldsLoadInterface interface {
		LoadWaterOlds(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type ResidentsLoadInterface interface {
		LoadResidents(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type SiteLoadInterface interface {
		LoadSite(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}

	return addressThenLoader[Q]{
		Mailers: thenLoadBuilder[Q](
			"Mailers",
			func(ctx context.Context, exec bob.Executor, retrieved MailersLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadMailers(ctx, exec, mods...)
			},
		),
		Pools: thenLoadBuilder[Q](
			"Pools",
			func(ctx context.Context, exec bob.Executor, retrieved PoolsLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadPools(ctx, exec, mods...)
			},
		),
		NuisanceOlds: thenLoadBuilder[Q](
			"NuisanceOlds",
			func(ctx context.Context, exec bob.Executor, retrieved NuisanceOldsLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadNuisanceOlds(ctx, exec, mods...)
			},
		),
		Reports: thenLoadBuilder[Q](
			"Reports",
			func(ctx context.Context, exec bob.Executor, retrieved ReportsLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadReports(ctx, exec, mods...)
			},
		),
		WaterOlds: thenLoadBuilder[Q](
			"WaterOlds",
			func(ctx context.Context, exec bob.Executor, retrieved WaterOldsLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadWaterOlds(ctx, exec, mods...)
			},
		),
		Residents: thenLoadBuilder[Q](
			"Residents",
			func(ctx context.Context, exec bob.Executor, retrieved ResidentsLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadResidents(ctx, exec, mods...)
			},
		),
		Site: thenLoadBuilder[Q](
			"Site",
			func(ctx context.Context, exec bob.Executor, retrieved SiteLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadSite(ctx, exec, mods...)
			},
		),
	}
}

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

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

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

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

	o.R.Mailers = related
	return nil
}

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

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

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

		o.R.Mailers = nil
	}

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

		for _, rel := range commsMailers {

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

			rel.R.Address = o

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

	return nil
}

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

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

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

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

	o.R.Pools = related
	return nil
}

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

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

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

		o.R.Pools = nil
	}

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

		for _, rel := range fileuploadPools {

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

			rel.R.Address = o

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

	return nil
}

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

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

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

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

	o.R.NuisanceOlds = related
	return nil
}

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

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

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

		o.R.NuisanceOlds = nil
	}

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

		for _, rel := range publicreportNuisanceOlds {

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

			rel.R.Address = o

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

	return nil
}

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

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

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

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

	o.R.Reports = related
	return nil
}

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

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

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

		o.R.Reports = nil
	}

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

		for _, rel := range publicreportReports {

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

			rel.R.Address = o

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

	return nil
}

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

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

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

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

	o.R.WaterOlds = related
	return nil
}

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

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

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

		o.R.WaterOlds = nil
	}

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

		for _, rel := range publicreportWaterOlds {

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

			rel.R.Address = o

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

	return nil
}

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

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

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

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

	o.R.Residents = related
	return nil
}

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

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

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

		o.R.Residents = nil
	}

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

		for _, rel := range residents {

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

			rel.R.Address = o

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

	return nil
}

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

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

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

	related.R.Address = o

	o.R.Site = related
	return nil
}

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

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

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

		for _, rel := range sites {

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

			rel.R.Address = o

			o.R.Site = rel
			break
		}
	}

	return nil
}
