// 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/stephenafamo/scan"
	enums "source.gleipnir.technology/Gleipnir/nidus-sync/db/enums"
)

// PublicreportWaterOld is an object representing the database table.
type PublicreportWaterOld struct {
	ID                     int32                              `db:"id,pk" `
	AccessComments         string                             `db:"access_comments" `
	AccessGate             bool                               `db:"access_gate" `
	AccessFence            bool                               `db:"access_fence" `
	AccessLocked           bool                               `db:"access_locked" `
	AccessDog              bool                               `db:"access_dog" `
	AccessOther            bool                               `db:"access_other" `
	AddressRaw             string                             `db:"address_raw" `
	AddressCountry         string                             `db:"address_country" `
	AddressPostalCode      string                             `db:"address_postal_code" `
	AddressLocality        string                             `db:"address_locality" `
	AddressStreet          string                             `db:"address_street" `
	AddressRegion          string                             `db:"address_region" `
	Comments               string                             `db:"comments" `
	Created                time.Time                          `db:"created" `
	H3cell                 null.Val[string]                   `db:"h3cell" `
	HasAdult               bool                               `db:"has_adult" `
	HasLarvae              bool                               `db:"has_larvae" `
	HasPupae               bool                               `db:"has_pupae" `
	MapZoom                float32                            `db:"map_zoom" `
	OwnerEmail             string                             `db:"owner_email" `
	OwnerName              string                             `db:"owner_name" `
	OwnerPhone             string                             `db:"owner_phone" `
	PublicID               string                             `db:"public_id" `
	ReporterEmail          string                             `db:"reporter_email" `
	ReporterName           string                             `db:"reporter_name" `
	ReporterPhone          string                             `db:"reporter_phone" `
	Status                 enums.PublicreportReportstatustype `db:"status" `
	OrganizationID         int32                              `db:"organization_id" `
	HasBackyardPermission  bool                               `db:"has_backyard_permission" `
	IsReporterConfidential bool                               `db:"is_reporter_confidential" `
	IsReporterOwner        bool                               `db:"is_reporter_owner" `
	ReporterContactConsent null.Val[bool]                     `db:"reporter_contact_consent" `
	Location               null.Val[string]                   `db:"location" `
	AddressNumber          string                             `db:"address_number" `
	AddressID              null.Val[int32]                    `db:"address_id" `
	Reviewed               null.Val[time.Time]                `db:"reviewed" `
	ReviewerID             null.Val[int32]                    `db:"reviewer_id" `

	R publicreportWaterOldR `db:"-" `
}

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

// PublicreportWaterOlds contains methods to work with the water_old table
var PublicreportWaterOlds = psql.NewTablex[*PublicreportWaterOld, PublicreportWaterOldSlice, *PublicreportWaterOldSetter]("publicreport", "water_old", buildPublicreportWaterOldColumns("publicreport.water_old"))

// PublicreportWaterOldsQuery is a query on the water_old table
type PublicreportWaterOldsQuery = *psql.ViewQuery[*PublicreportWaterOld, PublicreportWaterOldSlice]

// publicreportWaterOldR is where relationships are stored.
type publicreportWaterOldR struct {
	WaterNotifyEmailWaterOlds PublicreportNotifyEmailWaterOldSlice // publicreport.notify_email_water_old.notify_email_pool_pool_id_fkey
	WaterNotifyPhoneWaterOlds PublicreportNotifyPhoneWaterOldSlice // publicreport.notify_phone_water_old.notify_phone_pool_pool_id_fkey
	Images                    PublicreportImageSlice               // publicreport.water_image_old.pool_image_image_id_fkeypublicreport.water_image_old.pool_image_pool_id_fkey
	Address                   *Address                             // publicreport.water_old.pool_address_id_fkey
	Organization              *Organization                        // publicreport.water_old.pool_organization_id_fkey
	ReviewerUser              *User                                // publicreport.water_old.water_reviewer_id_fkey
}

func buildPublicreportWaterOldColumns(alias string) publicreportWaterOldColumns {
	return publicreportWaterOldColumns{
		ColumnsExpr: expr.NewColumnsExpr(
			"id", "access_comments", "access_gate", "access_fence", "access_locked", "access_dog", "access_other", "address_raw", "address_country", "address_postal_code", "address_locality", "address_street", "address_region", "comments", "created", "h3cell", "has_adult", "has_larvae", "has_pupae", "map_zoom", "owner_email", "owner_name", "owner_phone", "public_id", "reporter_email", "reporter_name", "reporter_phone", "status", "organization_id", "has_backyard_permission", "is_reporter_confidential", "is_reporter_owner", "reporter_contact_consent", "location", "address_number", "address_id", "reviewed", "reviewer_id",
		).WithParent("publicreport.water_old"),
		tableAlias:             alias,
		ID:                     psql.Quote(alias, "id"),
		AccessComments:         psql.Quote(alias, "access_comments"),
		AccessGate:             psql.Quote(alias, "access_gate"),
		AccessFence:            psql.Quote(alias, "access_fence"),
		AccessLocked:           psql.Quote(alias, "access_locked"),
		AccessDog:              psql.Quote(alias, "access_dog"),
		AccessOther:            psql.Quote(alias, "access_other"),
		AddressRaw:             psql.Quote(alias, "address_raw"),
		AddressCountry:         psql.Quote(alias, "address_country"),
		AddressPostalCode:      psql.Quote(alias, "address_postal_code"),
		AddressLocality:        psql.Quote(alias, "address_locality"),
		AddressStreet:          psql.Quote(alias, "address_street"),
		AddressRegion:          psql.Quote(alias, "address_region"),
		Comments:               psql.Quote(alias, "comments"),
		Created:                psql.Quote(alias, "created"),
		H3cell:                 psql.Quote(alias, "h3cell"),
		HasAdult:               psql.Quote(alias, "has_adult"),
		HasLarvae:              psql.Quote(alias, "has_larvae"),
		HasPupae:               psql.Quote(alias, "has_pupae"),
		MapZoom:                psql.Quote(alias, "map_zoom"),
		OwnerEmail:             psql.Quote(alias, "owner_email"),
		OwnerName:              psql.Quote(alias, "owner_name"),
		OwnerPhone:             psql.Quote(alias, "owner_phone"),
		PublicID:               psql.Quote(alias, "public_id"),
		ReporterEmail:          psql.Quote(alias, "reporter_email"),
		ReporterName:           psql.Quote(alias, "reporter_name"),
		ReporterPhone:          psql.Quote(alias, "reporter_phone"),
		Status:                 psql.Quote(alias, "status"),
		OrganizationID:         psql.Quote(alias, "organization_id"),
		HasBackyardPermission:  psql.Quote(alias, "has_backyard_permission"),
		IsReporterConfidential: psql.Quote(alias, "is_reporter_confidential"),
		IsReporterOwner:        psql.Quote(alias, "is_reporter_owner"),
		ReporterContactConsent: psql.Quote(alias, "reporter_contact_consent"),
		Location:               psql.Quote(alias, "location"),
		AddressNumber:          psql.Quote(alias, "address_number"),
		AddressID:              psql.Quote(alias, "address_id"),
		Reviewed:               psql.Quote(alias, "reviewed"),
		ReviewerID:             psql.Quote(alias, "reviewer_id"),
	}
}

type publicreportWaterOldColumns struct {
	expr.ColumnsExpr
	tableAlias             string
	ID                     psql.Expression
	AccessComments         psql.Expression
	AccessGate             psql.Expression
	AccessFence            psql.Expression
	AccessLocked           psql.Expression
	AccessDog              psql.Expression
	AccessOther            psql.Expression
	AddressRaw             psql.Expression
	AddressCountry         psql.Expression
	AddressPostalCode      psql.Expression
	AddressLocality        psql.Expression
	AddressStreet          psql.Expression
	AddressRegion          psql.Expression
	Comments               psql.Expression
	Created                psql.Expression
	H3cell                 psql.Expression
	HasAdult               psql.Expression
	HasLarvae              psql.Expression
	HasPupae               psql.Expression
	MapZoom                psql.Expression
	OwnerEmail             psql.Expression
	OwnerName              psql.Expression
	OwnerPhone             psql.Expression
	PublicID               psql.Expression
	ReporterEmail          psql.Expression
	ReporterName           psql.Expression
	ReporterPhone          psql.Expression
	Status                 psql.Expression
	OrganizationID         psql.Expression
	HasBackyardPermission  psql.Expression
	IsReporterConfidential psql.Expression
	IsReporterOwner        psql.Expression
	ReporterContactConsent psql.Expression
	Location               psql.Expression
	AddressNumber          psql.Expression
	AddressID              psql.Expression
	Reviewed               psql.Expression
	ReviewerID             psql.Expression
}

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

func (publicreportWaterOldColumns) AliasedAs(alias string) publicreportWaterOldColumns {
	return buildPublicreportWaterOldColumns(alias)
}

// PublicreportWaterOldSetter is used for insert/upsert/update operations
// All values are optional, and do not have to be set
// Generated columns are not included
type PublicreportWaterOldSetter struct {
	ID                     omit.Val[int32]                              `db:"id,pk" `
	AccessComments         omit.Val[string]                             `db:"access_comments" `
	AccessGate             omit.Val[bool]                               `db:"access_gate" `
	AccessFence            omit.Val[bool]                               `db:"access_fence" `
	AccessLocked           omit.Val[bool]                               `db:"access_locked" `
	AccessDog              omit.Val[bool]                               `db:"access_dog" `
	AccessOther            omit.Val[bool]                               `db:"access_other" `
	AddressRaw             omit.Val[string]                             `db:"address_raw" `
	AddressCountry         omit.Val[string]                             `db:"address_country" `
	AddressPostalCode      omit.Val[string]                             `db:"address_postal_code" `
	AddressLocality        omit.Val[string]                             `db:"address_locality" `
	AddressStreet          omit.Val[string]                             `db:"address_street" `
	AddressRegion          omit.Val[string]                             `db:"address_region" `
	Comments               omit.Val[string]                             `db:"comments" `
	Created                omit.Val[time.Time]                          `db:"created" `
	H3cell                 omitnull.Val[string]                         `db:"h3cell" `
	HasAdult               omit.Val[bool]                               `db:"has_adult" `
	HasLarvae              omit.Val[bool]                               `db:"has_larvae" `
	HasPupae               omit.Val[bool]                               `db:"has_pupae" `
	MapZoom                omit.Val[float32]                            `db:"map_zoom" `
	OwnerEmail             omit.Val[string]                             `db:"owner_email" `
	OwnerName              omit.Val[string]                             `db:"owner_name" `
	OwnerPhone             omit.Val[string]                             `db:"owner_phone" `
	PublicID               omit.Val[string]                             `db:"public_id" `
	ReporterEmail          omit.Val[string]                             `db:"reporter_email" `
	ReporterName           omit.Val[string]                             `db:"reporter_name" `
	ReporterPhone          omit.Val[string]                             `db:"reporter_phone" `
	Status                 omit.Val[enums.PublicreportReportstatustype] `db:"status" `
	OrganizationID         omit.Val[int32]                              `db:"organization_id" `
	HasBackyardPermission  omit.Val[bool]                               `db:"has_backyard_permission" `
	IsReporterConfidential omit.Val[bool]                               `db:"is_reporter_confidential" `
	IsReporterOwner        omit.Val[bool]                               `db:"is_reporter_owner" `
	ReporterContactConsent omitnull.Val[bool]                           `db:"reporter_contact_consent" `
	Location               omitnull.Val[string]                         `db:"location" `
	AddressNumber          omit.Val[string]                             `db:"address_number" `
	AddressID              omitnull.Val[int32]                          `db:"address_id" `
	Reviewed               omitnull.Val[time.Time]                      `db:"reviewed" `
	ReviewerID             omitnull.Val[int32]                          `db:"reviewer_id" `
}

func (s PublicreportWaterOldSetter) SetColumns() []string {
	vals := make([]string, 0, 38)
	if s.ID.IsValue() {
		vals = append(vals, "id")
	}
	if s.AccessComments.IsValue() {
		vals = append(vals, "access_comments")
	}
	if s.AccessGate.IsValue() {
		vals = append(vals, "access_gate")
	}
	if s.AccessFence.IsValue() {
		vals = append(vals, "access_fence")
	}
	if s.AccessLocked.IsValue() {
		vals = append(vals, "access_locked")
	}
	if s.AccessDog.IsValue() {
		vals = append(vals, "access_dog")
	}
	if s.AccessOther.IsValue() {
		vals = append(vals, "access_other")
	}
	if s.AddressRaw.IsValue() {
		vals = append(vals, "address_raw")
	}
	if s.AddressCountry.IsValue() {
		vals = append(vals, "address_country")
	}
	if s.AddressPostalCode.IsValue() {
		vals = append(vals, "address_postal_code")
	}
	if s.AddressLocality.IsValue() {
		vals = append(vals, "address_locality")
	}
	if s.AddressStreet.IsValue() {
		vals = append(vals, "address_street")
	}
	if s.AddressRegion.IsValue() {
		vals = append(vals, "address_region")
	}
	if s.Comments.IsValue() {
		vals = append(vals, "comments")
	}
	if s.Created.IsValue() {
		vals = append(vals, "created")
	}
	if !s.H3cell.IsUnset() {
		vals = append(vals, "h3cell")
	}
	if s.HasAdult.IsValue() {
		vals = append(vals, "has_adult")
	}
	if s.HasLarvae.IsValue() {
		vals = append(vals, "has_larvae")
	}
	if s.HasPupae.IsValue() {
		vals = append(vals, "has_pupae")
	}
	if s.MapZoom.IsValue() {
		vals = append(vals, "map_zoom")
	}
	if s.OwnerEmail.IsValue() {
		vals = append(vals, "owner_email")
	}
	if s.OwnerName.IsValue() {
		vals = append(vals, "owner_name")
	}
	if s.OwnerPhone.IsValue() {
		vals = append(vals, "owner_phone")
	}
	if s.PublicID.IsValue() {
		vals = append(vals, "public_id")
	}
	if s.ReporterEmail.IsValue() {
		vals = append(vals, "reporter_email")
	}
	if s.ReporterName.IsValue() {
		vals = append(vals, "reporter_name")
	}
	if s.ReporterPhone.IsValue() {
		vals = append(vals, "reporter_phone")
	}
	if s.Status.IsValue() {
		vals = append(vals, "status")
	}
	if s.OrganizationID.IsValue() {
		vals = append(vals, "organization_id")
	}
	if s.HasBackyardPermission.IsValue() {
		vals = append(vals, "has_backyard_permission")
	}
	if s.IsReporterConfidential.IsValue() {
		vals = append(vals, "is_reporter_confidential")
	}
	if s.IsReporterOwner.IsValue() {
		vals = append(vals, "is_reporter_owner")
	}
	if !s.ReporterContactConsent.IsUnset() {
		vals = append(vals, "reporter_contact_consent")
	}
	if !s.Location.IsUnset() {
		vals = append(vals, "location")
	}
	if s.AddressNumber.IsValue() {
		vals = append(vals, "address_number")
	}
	if !s.AddressID.IsUnset() {
		vals = append(vals, "address_id")
	}
	if !s.Reviewed.IsUnset() {
		vals = append(vals, "reviewed")
	}
	if !s.ReviewerID.IsUnset() {
		vals = append(vals, "reviewer_id")
	}
	return vals
}

func (s PublicreportWaterOldSetter) Overwrite(t *PublicreportWaterOld) {
	if s.ID.IsValue() {
		t.ID = s.ID.MustGet()
	}
	if s.AccessComments.IsValue() {
		t.AccessComments = s.AccessComments.MustGet()
	}
	if s.AccessGate.IsValue() {
		t.AccessGate = s.AccessGate.MustGet()
	}
	if s.AccessFence.IsValue() {
		t.AccessFence = s.AccessFence.MustGet()
	}
	if s.AccessLocked.IsValue() {
		t.AccessLocked = s.AccessLocked.MustGet()
	}
	if s.AccessDog.IsValue() {
		t.AccessDog = s.AccessDog.MustGet()
	}
	if s.AccessOther.IsValue() {
		t.AccessOther = s.AccessOther.MustGet()
	}
	if s.AddressRaw.IsValue() {
		t.AddressRaw = s.AddressRaw.MustGet()
	}
	if s.AddressCountry.IsValue() {
		t.AddressCountry = s.AddressCountry.MustGet()
	}
	if s.AddressPostalCode.IsValue() {
		t.AddressPostalCode = s.AddressPostalCode.MustGet()
	}
	if s.AddressLocality.IsValue() {
		t.AddressLocality = s.AddressLocality.MustGet()
	}
	if s.AddressStreet.IsValue() {
		t.AddressStreet = s.AddressStreet.MustGet()
	}
	if s.AddressRegion.IsValue() {
		t.AddressRegion = s.AddressRegion.MustGet()
	}
	if s.Comments.IsValue() {
		t.Comments = s.Comments.MustGet()
	}
	if s.Created.IsValue() {
		t.Created = s.Created.MustGet()
	}
	if !s.H3cell.IsUnset() {
		t.H3cell = s.H3cell.MustGetNull()
	}
	if s.HasAdult.IsValue() {
		t.HasAdult = s.HasAdult.MustGet()
	}
	if s.HasLarvae.IsValue() {
		t.HasLarvae = s.HasLarvae.MustGet()
	}
	if s.HasPupae.IsValue() {
		t.HasPupae = s.HasPupae.MustGet()
	}
	if s.MapZoom.IsValue() {
		t.MapZoom = s.MapZoom.MustGet()
	}
	if s.OwnerEmail.IsValue() {
		t.OwnerEmail = s.OwnerEmail.MustGet()
	}
	if s.OwnerName.IsValue() {
		t.OwnerName = s.OwnerName.MustGet()
	}
	if s.OwnerPhone.IsValue() {
		t.OwnerPhone = s.OwnerPhone.MustGet()
	}
	if s.PublicID.IsValue() {
		t.PublicID = s.PublicID.MustGet()
	}
	if s.ReporterEmail.IsValue() {
		t.ReporterEmail = s.ReporterEmail.MustGet()
	}
	if s.ReporterName.IsValue() {
		t.ReporterName = s.ReporterName.MustGet()
	}
	if s.ReporterPhone.IsValue() {
		t.ReporterPhone = s.ReporterPhone.MustGet()
	}
	if s.Status.IsValue() {
		t.Status = s.Status.MustGet()
	}
	if s.OrganizationID.IsValue() {
		t.OrganizationID = s.OrganizationID.MustGet()
	}
	if s.HasBackyardPermission.IsValue() {
		t.HasBackyardPermission = s.HasBackyardPermission.MustGet()
	}
	if s.IsReporterConfidential.IsValue() {
		t.IsReporterConfidential = s.IsReporterConfidential.MustGet()
	}
	if s.IsReporterOwner.IsValue() {
		t.IsReporterOwner = s.IsReporterOwner.MustGet()
	}
	if !s.ReporterContactConsent.IsUnset() {
		t.ReporterContactConsent = s.ReporterContactConsent.MustGetNull()
	}
	if !s.Location.IsUnset() {
		t.Location = s.Location.MustGetNull()
	}
	if s.AddressNumber.IsValue() {
		t.AddressNumber = s.AddressNumber.MustGet()
	}
	if !s.AddressID.IsUnset() {
		t.AddressID = s.AddressID.MustGetNull()
	}
	if !s.Reviewed.IsUnset() {
		t.Reviewed = s.Reviewed.MustGetNull()
	}
	if !s.ReviewerID.IsUnset() {
		t.ReviewerID = s.ReviewerID.MustGetNull()
	}
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return exprs
}

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

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

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

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

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

	return err
}

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

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

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

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

	return err
}

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

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

			return err
		}))

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

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

			return err
		}))

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

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

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

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

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

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

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

	o.copyMatchingRows(o2...)

	return nil
}

// WaterNotifyEmailWaterOlds starts a query for related objects on publicreport.notify_email_water_old
func (o *PublicreportWaterOld) WaterNotifyEmailWaterOlds(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportNotifyEmailWaterOldsQuery {
	return PublicreportNotifyEmailWaterOlds.Query(append(mods,
		sm.Where(PublicreportNotifyEmailWaterOlds.Columns.WaterID.EQ(psql.Arg(o.ID))),
	)...)
}

func (os PublicreportWaterOldSlice) WaterNotifyEmailWaterOlds(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportNotifyEmailWaterOldsQuery {
	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 PublicreportNotifyEmailWaterOlds.Query(append(mods,
		sm.Where(psql.Group(PublicreportNotifyEmailWaterOlds.Columns.WaterID).OP("IN", PKArgExpr)),
	)...)
}

// WaterNotifyPhoneWaterOlds starts a query for related objects on publicreport.notify_phone_water_old
func (o *PublicreportWaterOld) WaterNotifyPhoneWaterOlds(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportNotifyPhoneWaterOldsQuery {
	return PublicreportNotifyPhoneWaterOlds.Query(append(mods,
		sm.Where(PublicreportNotifyPhoneWaterOlds.Columns.WaterID.EQ(psql.Arg(o.ID))),
	)...)
}

func (os PublicreportWaterOldSlice) WaterNotifyPhoneWaterOlds(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportNotifyPhoneWaterOldsQuery {
	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 PublicreportNotifyPhoneWaterOlds.Query(append(mods,
		sm.Where(psql.Group(PublicreportNotifyPhoneWaterOlds.Columns.WaterID).OP("IN", PKArgExpr)),
	)...)
}

// Images starts a query for related objects on publicreport.image
func (o *PublicreportWaterOld) Images(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportImagesQuery {
	return PublicreportImages.Query(append(mods,
		sm.InnerJoin(PublicreportWaterImageOlds.NameAs()).On(
			PublicreportImages.Columns.ID.EQ(PublicreportWaterImageOlds.Columns.ImageID)),
		sm.Where(PublicreportWaterImageOlds.Columns.WaterID.EQ(psql.Arg(o.ID))),
	)...)
}

func (os PublicreportWaterOldSlice) Images(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportImagesQuery {
	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 PublicreportImages.Query(append(mods,
		sm.InnerJoin(PublicreportWaterImageOlds.NameAs()).On(
			PublicreportImages.Columns.ID.EQ(PublicreportWaterImageOlds.Columns.ImageID),
		),
		sm.Where(psql.Group(PublicreportWaterImageOlds.Columns.WaterID).OP("IN", PKArgExpr)),
	)...)
}

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

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

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

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

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

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

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

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

func insertPublicreportWaterOldWaterNotifyEmailWaterOlds0(ctx context.Context, exec bob.Executor, publicreportNotifyEmailWaterOlds1 []*PublicreportNotifyEmailWaterOldSetter, publicreportWaterOld0 *PublicreportWaterOld) (PublicreportNotifyEmailWaterOldSlice, error) {
	for i := range publicreportNotifyEmailWaterOlds1 {
		publicreportNotifyEmailWaterOlds1[i].WaterID = omit.From(publicreportWaterOld0.ID)
	}

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

	return ret, nil
}

func attachPublicreportWaterOldWaterNotifyEmailWaterOlds0(ctx context.Context, exec bob.Executor, count int, publicreportNotifyEmailWaterOlds1 PublicreportNotifyEmailWaterOldSlice, publicreportWaterOld0 *PublicreportWaterOld) (PublicreportNotifyEmailWaterOldSlice, error) {
	setter := &PublicreportNotifyEmailWaterOldSetter{
		WaterID: omit.From(publicreportWaterOld0.ID),
	}

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

	return publicreportNotifyEmailWaterOlds1, nil
}

func (publicreportWaterOld0 *PublicreportWaterOld) InsertWaterNotifyEmailWaterOlds(ctx context.Context, exec bob.Executor, related ...*PublicreportNotifyEmailWaterOldSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	publicreportNotifyEmailWaterOlds1, err := insertPublicreportWaterOldWaterNotifyEmailWaterOlds0(ctx, exec, related, publicreportWaterOld0)
	if err != nil {
		return err
	}

	publicreportWaterOld0.R.WaterNotifyEmailWaterOlds = append(publicreportWaterOld0.R.WaterNotifyEmailWaterOlds, publicreportNotifyEmailWaterOlds1...)

	for _, rel := range publicreportNotifyEmailWaterOlds1 {
		rel.R.WaterWaterOld = publicreportWaterOld0
	}
	return nil
}

func (publicreportWaterOld0 *PublicreportWaterOld) AttachWaterNotifyEmailWaterOlds(ctx context.Context, exec bob.Executor, related ...*PublicreportNotifyEmailWaterOld) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	publicreportNotifyEmailWaterOlds1 := PublicreportNotifyEmailWaterOldSlice(related)

	_, err = attachPublicreportWaterOldWaterNotifyEmailWaterOlds0(ctx, exec, len(related), publicreportNotifyEmailWaterOlds1, publicreportWaterOld0)
	if err != nil {
		return err
	}

	publicreportWaterOld0.R.WaterNotifyEmailWaterOlds = append(publicreportWaterOld0.R.WaterNotifyEmailWaterOlds, publicreportNotifyEmailWaterOlds1...)

	for _, rel := range related {
		rel.R.WaterWaterOld = publicreportWaterOld0
	}

	return nil
}

func insertPublicreportWaterOldWaterNotifyPhoneWaterOlds0(ctx context.Context, exec bob.Executor, publicreportNotifyPhoneWaterOlds1 []*PublicreportNotifyPhoneWaterOldSetter, publicreportWaterOld0 *PublicreportWaterOld) (PublicreportNotifyPhoneWaterOldSlice, error) {
	for i := range publicreportNotifyPhoneWaterOlds1 {
		publicreportNotifyPhoneWaterOlds1[i].WaterID = omit.From(publicreportWaterOld0.ID)
	}

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

	return ret, nil
}

func attachPublicreportWaterOldWaterNotifyPhoneWaterOlds0(ctx context.Context, exec bob.Executor, count int, publicreportNotifyPhoneWaterOlds1 PublicreportNotifyPhoneWaterOldSlice, publicreportWaterOld0 *PublicreportWaterOld) (PublicreportNotifyPhoneWaterOldSlice, error) {
	setter := &PublicreportNotifyPhoneWaterOldSetter{
		WaterID: omit.From(publicreportWaterOld0.ID),
	}

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

	return publicreportNotifyPhoneWaterOlds1, nil
}

func (publicreportWaterOld0 *PublicreportWaterOld) InsertWaterNotifyPhoneWaterOlds(ctx context.Context, exec bob.Executor, related ...*PublicreportNotifyPhoneWaterOldSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	publicreportNotifyPhoneWaterOlds1, err := insertPublicreportWaterOldWaterNotifyPhoneWaterOlds0(ctx, exec, related, publicreportWaterOld0)
	if err != nil {
		return err
	}

	publicreportWaterOld0.R.WaterNotifyPhoneWaterOlds = append(publicreportWaterOld0.R.WaterNotifyPhoneWaterOlds, publicreportNotifyPhoneWaterOlds1...)

	for _, rel := range publicreportNotifyPhoneWaterOlds1 {
		rel.R.WaterWaterOld = publicreportWaterOld0
	}
	return nil
}

func (publicreportWaterOld0 *PublicreportWaterOld) AttachWaterNotifyPhoneWaterOlds(ctx context.Context, exec bob.Executor, related ...*PublicreportNotifyPhoneWaterOld) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	publicreportNotifyPhoneWaterOlds1 := PublicreportNotifyPhoneWaterOldSlice(related)

	_, err = attachPublicreportWaterOldWaterNotifyPhoneWaterOlds0(ctx, exec, len(related), publicreportNotifyPhoneWaterOlds1, publicreportWaterOld0)
	if err != nil {
		return err
	}

	publicreportWaterOld0.R.WaterNotifyPhoneWaterOlds = append(publicreportWaterOld0.R.WaterNotifyPhoneWaterOlds, publicreportNotifyPhoneWaterOlds1...)

	for _, rel := range related {
		rel.R.WaterWaterOld = publicreportWaterOld0
	}

	return nil
}

func attachPublicreportWaterOldImages0(ctx context.Context, exec bob.Executor, count int, publicreportWaterOld0 *PublicreportWaterOld, publicreportImages2 PublicreportImageSlice) (PublicreportWaterImageOldSlice, error) {
	setters := make([]*PublicreportWaterImageOldSetter, count)
	for i := range count {
		setters[i] = &PublicreportWaterImageOldSetter{
			WaterID: omit.From(publicreportWaterOld0.ID),
			ImageID: omit.From(publicreportImages2[i].ID),
		}
	}

	publicreportWaterImageOlds1, err := PublicreportWaterImageOlds.Insert(bob.ToMods(setters...)).All(ctx, exec)
	if err != nil {
		return nil, fmt.Errorf("attachPublicreportWaterOldImages0: %w", err)
	}

	return publicreportWaterImageOlds1, nil
}

func (publicreportWaterOld0 *PublicreportWaterOld) InsertImages(ctx context.Context, exec bob.Executor, related ...*PublicreportImageSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	inserted, err := PublicreportImages.Insert(bob.ToMods(related...)).All(ctx, exec)
	if err != nil {
		return fmt.Errorf("inserting related objects: %w", err)
	}
	publicreportImages2 := PublicreportImageSlice(inserted)

	_, err = attachPublicreportWaterOldImages0(ctx, exec, len(related), publicreportWaterOld0, publicreportImages2)
	if err != nil {
		return err
	}

	publicreportWaterOld0.R.Images = append(publicreportWaterOld0.R.Images, publicreportImages2...)

	for _, rel := range publicreportImages2 {
		rel.R.WaterOlds = append(rel.R.WaterOlds, publicreportWaterOld0)
	}
	return nil
}

func (publicreportWaterOld0 *PublicreportWaterOld) AttachImages(ctx context.Context, exec bob.Executor, related ...*PublicreportImage) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	publicreportImages2 := PublicreportImageSlice(related)

	_, err = attachPublicreportWaterOldImages0(ctx, exec, len(related), publicreportWaterOld0, publicreportImages2)
	if err != nil {
		return err
	}

	publicreportWaterOld0.R.Images = append(publicreportWaterOld0.R.Images, publicreportImages2...)

	for _, rel := range related {
		rel.R.WaterOlds = append(rel.R.WaterOlds, publicreportWaterOld0)
	}

	return nil
}

func attachPublicreportWaterOldAddress0(ctx context.Context, exec bob.Executor, count int, publicreportWaterOld0 *PublicreportWaterOld, address1 *Address) (*PublicreportWaterOld, error) {
	setter := &PublicreportWaterOldSetter{
		AddressID: omitnull.From(address1.ID),
	}

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

	return publicreportWaterOld0, nil
}

func (publicreportWaterOld0 *PublicreportWaterOld) InsertAddress(ctx context.Context, exec bob.Executor, related *AddressSetter) error {
	var err error

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

	_, err = attachPublicreportWaterOldAddress0(ctx, exec, 1, publicreportWaterOld0, address1)
	if err != nil {
		return err
	}

	publicreportWaterOld0.R.Address = address1

	address1.R.WaterOlds = append(address1.R.WaterOlds, publicreportWaterOld0)

	return nil
}

func (publicreportWaterOld0 *PublicreportWaterOld) AttachAddress(ctx context.Context, exec bob.Executor, address1 *Address) error {
	var err error

	_, err = attachPublicreportWaterOldAddress0(ctx, exec, 1, publicreportWaterOld0, address1)
	if err != nil {
		return err
	}

	publicreportWaterOld0.R.Address = address1

	address1.R.WaterOlds = append(address1.R.WaterOlds, publicreportWaterOld0)

	return nil
}

func attachPublicreportWaterOldOrganization0(ctx context.Context, exec bob.Executor, count int, publicreportWaterOld0 *PublicreportWaterOld, organization1 *Organization) (*PublicreportWaterOld, error) {
	setter := &PublicreportWaterOldSetter{
		OrganizationID: omit.From(organization1.ID),
	}

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

	return publicreportWaterOld0, nil
}

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

	publicreportWaterOld0.R.Organization = organization1

	organization1.R.WaterOlds = append(organization1.R.WaterOlds, publicreportWaterOld0)

	return nil
}

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

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

	publicreportWaterOld0.R.Organization = organization1

	organization1.R.WaterOlds = append(organization1.R.WaterOlds, publicreportWaterOld0)

	return nil
}

func attachPublicreportWaterOldReviewerUser0(ctx context.Context, exec bob.Executor, count int, publicreportWaterOld0 *PublicreportWaterOld, user1 *User) (*PublicreportWaterOld, error) {
	setter := &PublicreportWaterOldSetter{
		ReviewerID: omitnull.From(user1.ID),
	}

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

	return publicreportWaterOld0, nil
}

func (publicreportWaterOld0 *PublicreportWaterOld) InsertReviewerUser(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 = attachPublicreportWaterOldReviewerUser0(ctx, exec, 1, publicreportWaterOld0, user1)
	if err != nil {
		return err
	}

	publicreportWaterOld0.R.ReviewerUser = user1

	user1.R.ReviewerWaterOlds = append(user1.R.ReviewerWaterOlds, publicreportWaterOld0)

	return nil
}

func (publicreportWaterOld0 *PublicreportWaterOld) AttachReviewerUser(ctx context.Context, exec bob.Executor, user1 *User) error {
	var err error

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

	publicreportWaterOld0.R.ReviewerUser = user1

	user1.R.ReviewerWaterOlds = append(user1.R.ReviewerWaterOlds, publicreportWaterOld0)

	return nil
}

type publicreportWaterOldWhere[Q psql.Filterable] struct {
	ID                     psql.WhereMod[Q, int32]
	AccessComments         psql.WhereMod[Q, string]
	AccessGate             psql.WhereMod[Q, bool]
	AccessFence            psql.WhereMod[Q, bool]
	AccessLocked           psql.WhereMod[Q, bool]
	AccessDog              psql.WhereMod[Q, bool]
	AccessOther            psql.WhereMod[Q, bool]
	AddressRaw             psql.WhereMod[Q, string]
	AddressCountry         psql.WhereMod[Q, string]
	AddressPostalCode      psql.WhereMod[Q, string]
	AddressLocality        psql.WhereMod[Q, string]
	AddressStreet          psql.WhereMod[Q, string]
	AddressRegion          psql.WhereMod[Q, string]
	Comments               psql.WhereMod[Q, string]
	Created                psql.WhereMod[Q, time.Time]
	H3cell                 psql.WhereNullMod[Q, string]
	HasAdult               psql.WhereMod[Q, bool]
	HasLarvae              psql.WhereMod[Q, bool]
	HasPupae               psql.WhereMod[Q, bool]
	MapZoom                psql.WhereMod[Q, float32]
	OwnerEmail             psql.WhereMod[Q, string]
	OwnerName              psql.WhereMod[Q, string]
	OwnerPhone             psql.WhereMod[Q, string]
	PublicID               psql.WhereMod[Q, string]
	ReporterEmail          psql.WhereMod[Q, string]
	ReporterName           psql.WhereMod[Q, string]
	ReporterPhone          psql.WhereMod[Q, string]
	Status                 psql.WhereMod[Q, enums.PublicreportReportstatustype]
	OrganizationID         psql.WhereMod[Q, int32]
	HasBackyardPermission  psql.WhereMod[Q, bool]
	IsReporterConfidential psql.WhereMod[Q, bool]
	IsReporterOwner        psql.WhereMod[Q, bool]
	ReporterContactConsent psql.WhereNullMod[Q, bool]
	Location               psql.WhereNullMod[Q, string]
	AddressNumber          psql.WhereMod[Q, string]
	AddressID              psql.WhereNullMod[Q, int32]
	Reviewed               psql.WhereNullMod[Q, time.Time]
	ReviewerID             psql.WhereNullMod[Q, int32]
}

func (publicreportWaterOldWhere[Q]) AliasedAs(alias string) publicreportWaterOldWhere[Q] {
	return buildPublicreportWaterOldWhere[Q](buildPublicreportWaterOldColumns(alias))
}

func buildPublicreportWaterOldWhere[Q psql.Filterable](cols publicreportWaterOldColumns) publicreportWaterOldWhere[Q] {
	return publicreportWaterOldWhere[Q]{
		ID:                     psql.Where[Q, int32](cols.ID),
		AccessComments:         psql.Where[Q, string](cols.AccessComments),
		AccessGate:             psql.Where[Q, bool](cols.AccessGate),
		AccessFence:            psql.Where[Q, bool](cols.AccessFence),
		AccessLocked:           psql.Where[Q, bool](cols.AccessLocked),
		AccessDog:              psql.Where[Q, bool](cols.AccessDog),
		AccessOther:            psql.Where[Q, bool](cols.AccessOther),
		AddressRaw:             psql.Where[Q, string](cols.AddressRaw),
		AddressCountry:         psql.Where[Q, string](cols.AddressCountry),
		AddressPostalCode:      psql.Where[Q, string](cols.AddressPostalCode),
		AddressLocality:        psql.Where[Q, string](cols.AddressLocality),
		AddressStreet:          psql.Where[Q, string](cols.AddressStreet),
		AddressRegion:          psql.Where[Q, string](cols.AddressRegion),
		Comments:               psql.Where[Q, string](cols.Comments),
		Created:                psql.Where[Q, time.Time](cols.Created),
		H3cell:                 psql.WhereNull[Q, string](cols.H3cell),
		HasAdult:               psql.Where[Q, bool](cols.HasAdult),
		HasLarvae:              psql.Where[Q, bool](cols.HasLarvae),
		HasPupae:               psql.Where[Q, bool](cols.HasPupae),
		MapZoom:                psql.Where[Q, float32](cols.MapZoom),
		OwnerEmail:             psql.Where[Q, string](cols.OwnerEmail),
		OwnerName:              psql.Where[Q, string](cols.OwnerName),
		OwnerPhone:             psql.Where[Q, string](cols.OwnerPhone),
		PublicID:               psql.Where[Q, string](cols.PublicID),
		ReporterEmail:          psql.Where[Q, string](cols.ReporterEmail),
		ReporterName:           psql.Where[Q, string](cols.ReporterName),
		ReporterPhone:          psql.Where[Q, string](cols.ReporterPhone),
		Status:                 psql.Where[Q, enums.PublicreportReportstatustype](cols.Status),
		OrganizationID:         psql.Where[Q, int32](cols.OrganizationID),
		HasBackyardPermission:  psql.Where[Q, bool](cols.HasBackyardPermission),
		IsReporterConfidential: psql.Where[Q, bool](cols.IsReporterConfidential),
		IsReporterOwner:        psql.Where[Q, bool](cols.IsReporterOwner),
		ReporterContactConsent: psql.WhereNull[Q, bool](cols.ReporterContactConsent),
		Location:               psql.WhereNull[Q, string](cols.Location),
		AddressNumber:          psql.Where[Q, string](cols.AddressNumber),
		AddressID:              psql.WhereNull[Q, int32](cols.AddressID),
		Reviewed:               psql.WhereNull[Q, time.Time](cols.Reviewed),
		ReviewerID:             psql.WhereNull[Q, int32](cols.ReviewerID),
	}
}

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

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

		o.R.WaterNotifyEmailWaterOlds = rels

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

		o.R.WaterNotifyPhoneWaterOlds = rels

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

		o.R.Images = rels

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

		o.R.Address = rel

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

		o.R.Organization = rel

		if rel != nil {
			rel.R.WaterOlds = PublicreportWaterOldSlice{o}
		}
		return nil
	case "ReviewerUser":
		rel, ok := retrieved.(*User)
		if !ok {
			return fmt.Errorf("publicreportWaterOld cannot load %T as %q", retrieved, name)
		}

		o.R.ReviewerUser = rel

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

type publicreportWaterOldPreloader struct {
	Address      func(...psql.PreloadOption) psql.Preloader
	Organization func(...psql.PreloadOption) psql.Preloader
	ReviewerUser func(...psql.PreloadOption) psql.Preloader
}

func buildPublicreportWaterOldPreloader() publicreportWaterOldPreloader {
	return publicreportWaterOldPreloader{
		Address: func(opts ...psql.PreloadOption) psql.Preloader {
			return psql.Preload[*Address, AddressSlice](psql.PreloadRel{
				Name: "Address",
				Sides: []psql.PreloadSide{
					{
						From:        PublicreportWaterOlds,
						To:          Addresses,
						FromColumns: []string{"address_id"},
						ToColumns:   []string{"id"},
					},
				},
			}, Addresses.Columns.Names(), opts...)
		},
		Organization: func(opts ...psql.PreloadOption) psql.Preloader {
			return psql.Preload[*Organization, OrganizationSlice](psql.PreloadRel{
				Name: "Organization",
				Sides: []psql.PreloadSide{
					{
						From:        PublicreportWaterOlds,
						To:          Organizations,
						FromColumns: []string{"organization_id"},
						ToColumns:   []string{"id"},
					},
				},
			}, Organizations.Columns.Names(), opts...)
		},
		ReviewerUser: func(opts ...psql.PreloadOption) psql.Preloader {
			return psql.Preload[*User, UserSlice](psql.PreloadRel{
				Name: "ReviewerUser",
				Sides: []psql.PreloadSide{
					{
						From:        PublicreportWaterOlds,
						To:          Users,
						FromColumns: []string{"reviewer_id"},
						ToColumns:   []string{"id"},
					},
				},
			}, Users.Columns.Names(), opts...)
		},
	}
}

type publicreportWaterOldThenLoader[Q orm.Loadable] struct {
	WaterNotifyEmailWaterOlds func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	WaterNotifyPhoneWaterOlds func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	Images                    func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	Address                   func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	Organization              func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	ReviewerUser              func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
}

func buildPublicreportWaterOldThenLoader[Q orm.Loadable]() publicreportWaterOldThenLoader[Q] {
	type WaterNotifyEmailWaterOldsLoadInterface interface {
		LoadWaterNotifyEmailWaterOlds(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type WaterNotifyPhoneWaterOldsLoadInterface interface {
		LoadWaterNotifyPhoneWaterOlds(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type ImagesLoadInterface interface {
		LoadImages(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type AddressLoadInterface interface {
		LoadAddress(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type OrganizationLoadInterface interface {
		LoadOrganization(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type ReviewerUserLoadInterface interface {
		LoadReviewerUser(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}

	return publicreportWaterOldThenLoader[Q]{
		WaterNotifyEmailWaterOlds: thenLoadBuilder[Q](
			"WaterNotifyEmailWaterOlds",
			func(ctx context.Context, exec bob.Executor, retrieved WaterNotifyEmailWaterOldsLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadWaterNotifyEmailWaterOlds(ctx, exec, mods...)
			},
		),
		WaterNotifyPhoneWaterOlds: thenLoadBuilder[Q](
			"WaterNotifyPhoneWaterOlds",
			func(ctx context.Context, exec bob.Executor, retrieved WaterNotifyPhoneWaterOldsLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadWaterNotifyPhoneWaterOlds(ctx, exec, mods...)
			},
		),
		Images: thenLoadBuilder[Q](
			"Images",
			func(ctx context.Context, exec bob.Executor, retrieved ImagesLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadImages(ctx, exec, mods...)
			},
		),
		Address: thenLoadBuilder[Q](
			"Address",
			func(ctx context.Context, exec bob.Executor, retrieved AddressLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadAddress(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...)
			},
		),
		ReviewerUser: thenLoadBuilder[Q](
			"ReviewerUser",
			func(ctx context.Context, exec bob.Executor, retrieved ReviewerUserLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadReviewerUser(ctx, exec, mods...)
			},
		),
	}
}

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

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

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

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

	o.R.WaterNotifyEmailWaterOlds = related
	return nil
}

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

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

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

		o.R.WaterNotifyEmailWaterOlds = nil
	}

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

		for _, rel := range publicreportNotifyEmailWaterOlds {

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

			rel.R.WaterWaterOld = o

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

	return nil
}

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

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

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

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

	o.R.WaterNotifyPhoneWaterOlds = related
	return nil
}

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

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

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

		o.R.WaterNotifyPhoneWaterOlds = nil
	}

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

		for _, rel := range publicreportNotifyPhoneWaterOlds {

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

			rel.R.WaterWaterOld = o

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

	return nil
}

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

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

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

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

	o.R.Images = related
	return nil
}

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

	// since we are changing the columns, we need to check if the original columns were set or add the defaults
	sq := dialect.SelectQuery{}
	for _, mod := range mods {
		mod.Apply(&sq)
	}

	if len(sq.SelectList.Columns) == 0 {
		mods = append(mods, sm.Columns(PublicreportImages.Columns))
	}

	q := os.Images(append(
		mods,
		sm.Columns(PublicreportWaterImageOlds.Columns.WaterID.As("related_publicreport.water_old.ID")),
	)...)

	IDSlice := []int32{}

	mapper := scan.Mod(scan.StructMapper[*PublicreportImage](), func(ctx context.Context, cols []string) (scan.BeforeFunc, func(any, any) error) {
		return func(row *scan.Row) (any, error) {
				IDSlice = append(IDSlice, *new(int32))
				row.ScheduleScanByName("related_publicreport.water_old.ID", &IDSlice[len(IDSlice)-1])

				return nil, nil
			},
			func(any, any) error {
				return nil
			}
	})

	publicreportImages, err := bob.Allx[bob.SliceTransformer[*PublicreportImage, PublicreportImageSlice]](ctx, exec, q, mapper)
	if err != nil {
		return err
	}

	for _, o := range os {
		o.R.Images = nil
	}

	for _, o := range os {
		for i, rel := range publicreportImages {
			if !(o.ID == IDSlice[i]) {
				continue
			}

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

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

	return nil
}

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

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

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

	related.R.WaterOlds = PublicreportWaterOldSlice{o}

	o.R.Address = related
	return nil
}

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

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

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

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

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

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

			o.R.Address = rel
			break
		}
	}

	return nil
}

// LoadOrganization loads the publicreportWaterOld's Organization into the .R struct
func (o *PublicreportWaterOld) 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.WaterOlds = PublicreportWaterOldSlice{o}

	o.R.Organization = related
	return nil
}

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

			o.R.Organization = rel
			break
		}
	}

	return nil
}

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

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

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

	related.R.ReviewerWaterOlds = PublicreportWaterOldSlice{o}

	o.R.ReviewerUser = related
	return nil
}

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

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

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

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

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

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

			o.R.ReviewerUser = rel
			break
		}
	}

	return nil
}
