Skip to content

Conversation

@atzedus
Copy link

@atzedus atzedus commented Feb 1, 2026

Add Unqualified() method to generated column structures

Summary

This PR adds a new Unqualified() method to generated column structures that returns columns without table alias/prefix. This is useful when you need to reference column names without table qualification, for example in INSERT ... ON CONFLICT clauses or when building dynamic queries.

Changes

  • Added Unqualified() method to generated *Columns structures
  • Modified buildXColumns() function to conditionally call WithParent() only when tableName is not empty
  • Renamed parameter from alias to tableName for better clarity

Example

Before this change, columns were always qualified with table name:

Orders.Columns.ID  // generates: "orders"."id"

Now you can get unqualified column names:

Orders.Columns.Unqualified().ID  // generates: "id"

Use Case

This is particularly useful for ON CONFLICT clauses where you need column names without table prefix:

psql.Insert(...).
    OnConflict(Orders.Columns.Unqualified().ID).
    DoUpdate(...)

@stephenafamo
Copy link
Owner

If Unqualified is now the same as calling AliasedAs(""), do we really need an additional method?

I try to avoid adding new methods so there are less avenues for breaking changes

@atzedus
Copy link
Author

atzedus commented Feb 1, 2026

If Unqualified is now the same as calling AliasedAs(""), do we really need an additional method?

I try to avoid adding new methods so there are less avenues for breaking changes

You're right that Unqualified() is functionally equivalent to AliasedAs(""). However, I believe there are good reasons to keep it:

  1. Semantic clarity

Unqualified() clearly expresses intent, while AliasedAs("") looks like a magic value or potential bug. When reading code:

// Clear intent - "give me columns without table prefix"
Orders.Columns.Unqualified()

// Confusing - "why empty string? Is this a mistake?"
Orders.Columns.AliasedAs("")
  1. It affects ColumnsExpr methods, not just individual column expressions

The key difference is that Unqualified() returns columns where ColumnsExpr.parent is empty. This affects methods like Only(), Except(), WithPrefix() that operate on the embedded ColumnsExpr:

// With Unqualified() - generates: "id", "name" (no table prefix)
Orders.Columns.Unqualified().Only("id", "name")

// The parent slice is empty, so WriteSQL produces clean column list
// Useful for INSERT column lists, ON CONFLICT targets, etc.
  1. Discoverability

Users looking for "how to get columns without table name" will find Unqualified() through autocomplete. They won't think to try AliasedAs("").

That said, I understand your concern about API surface. If you prefer, we could:

  • Document AliasedAs("") as the way to get unqualified columns
  • Or keep Unqualified() as a convenience method that's unlikely to need breaking changes

What do you think?

@stephenafamo
Copy link
Owner

Alright, let's add Unqualified()

Copy link
Owner

@stephenafamo stephenafamo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a little nitpick

@atzedus atzedus force-pushed the names-only-for-gen-templates branch 2 times, most recently from d37e152 to b8949b4 Compare February 1, 2026 20:50
@atzedus atzedus force-pushed the names-only-for-gen-templates branch from b8949b4 to 93ea41b Compare February 1, 2026 20:52
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