Skip to content

Conversation

@fr33r
Copy link
Collaborator

@fr33r fr33r commented May 17, 2025

Description

These changes adapt the SELECT query generation to include the table alias in the column list, as intended.

Rationale

This essentially was a bug - the alias was being included in the columns listed within the WHERE clause, but not for the columns in the SELECT portion.

Suggested Version

v1.5.0

Example Usage

package main

import (
	"fmt"
	"time"

	"github.com/freerware/morph"
)

type Starship struct {
	ID               string
	Name             string
	ManufacturedAt   time.Time
	DecommissionedAt *time.Time
	AccessCode       string `db:"-"`
}

func main() {
	s := Starship{
		ID:               "123",
		Name:             "Millennium Falcon",
		ManufacturedAt:   time.Now(),
		DecommissionedAt: nil,
		AccessCode:       "secret",
	}

	table := morph.Must(morph.Reflect(s, morph.WithTag("db"), morph.WithTableAlias("SHIP")))
	fmt.Println(table.SelectQuery()) // SELECT SHIP.decommissioned_at, SHIP.id, SHIP.name FROM starships AS SHIP WHERE 1=1 AND SHIP.id = ?;
}

@fr33r fr33r self-assigned this May 17, 2025
@fr33r fr33r merged commit df775d6 into master May 17, 2025
8 checks passed
@fr33r fr33r deleted the freer/add-alias-for-select branch May 18, 2025 05:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants