Skip to content

feat(dialect): add NameExpr/NameAsExpr and fix schema column qualifiers#692

Merged
stephenafamo merged 1 commit into
stephenafamo:mainfrom
atzedus:feat/view-name-expr
May 22, 2026
Merged

feat(dialect): add NameExpr/NameAsExpr and fix schema column qualifiers#692
stephenafamo merged 1 commit into
stephenafamo:mainfrom
atzedus:feat/view-name-expr

Conversation

@atzedus
Copy link
Copy Markdown
Contributor

@atzedus atzedus commented May 22, 2026

This PR completes the View/Table naming API started in #691 and fixes a codegen bug where generated column prefixes could disagree with the table alias used in SQL.

  • BREAKING: View / Table Name() now returns the bare table or view name as a string. SQL builders should use NameExpr() (formerly Name()) and NameAsExpr() (formerly NameAs()).
  • Added: Schema() on PostgreSQL and SQLite View types (MySQL is unchanged — no schema/database qualification on models).
  • Fixed: Introduced tableColumnAlias in codegen so build*Columns, pkEQ, and pkIN use the same qualifier as View.Alias() when schema is set at construction (schema.table instead of a bare table.Key such as users).
  • Updated codegen templates, orm.Preload, factory test templates, CHANGELOG, and view/table docs.

Codegen fix: column qualifiers vs table alias

The bug is not about renaming columns or tables — it is about which prefix codegen attaches to the same users.id when the model is built with a schema.

Assume the same table everywhere:

Field Value
Table users
Column id
Generated model models.Users

Only the schema at construction differs.

Case A — shared_schema (schema empty at codegen)

Codegen emits:

var Users = psql.NewTablex(..., "", "users", buildUsersColumns("users"), ...)

Runtime:

Value
Users.Schema() ""
Users.Name() "users"
Users.Alias() "users"
Users.Columns.ID "users"."id"
Users.NameAsExpr() "users" AS "users"

Manual query:

psql.Select(
    sm.From(models.Users.NameAsExpr()),
    sm.Where(models.Users.Columns.ID.EQ(1)),
)
SELECT "users"."id"
FROM "users" AS "users"
WHERE ("users"."id" = $1)

Prefix users is consistent everywhere. No bugtable.Key ("users") matches Alias().


Case B — non-shared schema (schema billing at codegen)

Codegen emits:

var Users = psql.NewTablex(..., "billing", "users", buildUsersColumns(...), ...)

Runtime (dialect):

Value
Users.Schema() "billing"
Users.Name() "users"
Users.Alias() "billing.users"
Users.NameAsExpr() "billing"."users" AS "billing.users"
Users.Query() columns (via View.ColumnsExpr) "billing.users"."id"

Before this PR — codegen passed table.Key ("users") into buildUsersColumns and pkEQ:

Value
buildUsersColumns("users") "users"."id"
pkEQ() Quote("users", "id")

Manual query with the same models.Users and same id:

psql.Select(
    sm.From(models.Users.NameAsExpr()),
    sm.Where(models.Users.Columns.ID.EQ(1)),
)
SELECT "users"."id"
FROM "billing"."users" AS "billing.users"
WHERE ("users"."id" = $1)

Users.Query() alone looked fine (it uses View.ColumnsExpr with parent "billing.users"), but Users.Columns.ID and pkEQ() pointed at "users"."id" while the SQL alias was "billing.users".

After this PRtableColumnAlias("billing", "users", "users")"billing.users":

var Users = psql.NewTablex(..., "billing", "users",
    buildUsersColumns("billing.users"), ...)
Value
Users.Columns.ID "billing.users"."id"
pkEQ() Quote("billing.users", "id")

Same manual query:

SELECT "billing.users"."id"
FROM "billing"."users" AS "billing.users"
WHERE ("billing.users"."id" = $1)

Same table users, same column id — only the schema argument changes the prefix, and codegen now matches View.Alias().

Migration

Before (v0.44.0) After
Users.Name() in sm.From, joins, etc. Users.NameExpr()
Users.NameAs() Users.NameAsExpr()
Need qualified name as Expression NameExpr() / NameAsExpr()
Need unqualified name string Users.Name()
Schema on psql/sqlite Users.Schema()

Regenerate models after upgrading bobgen.

BREAKING CHANGE: View and Table Name() now return the bare table or view
name as a string. Use NameExpr() and NameAsExpr() in query builders
(they replace the former Name() and NameAs() expression methods).

- Add Schema() on PostgreSQL and SQLite View types
- Add tableColumnAlias codegen helper so build*Columns, pkEQ, and pkIN
  match View.Alias() when schema is set at construction
- Update codegen templates, orm.Preload, factory tests, and docs
@stephenafamo
Copy link
Copy Markdown
Owner

This looks good.

I wonder if there is a way to prevent compilation if the models are generated with a version of Bob that is different from the runtime.

@atzedus
Copy link
Copy Markdown
Contributor Author

atzedus commented May 22, 2026

Good question. I think, that exact semver equality at compile time isn’t really doable.

What we can do is a codegen API version: like this does protobuf - https://pkg.go.dev/google.golang.org/protobuf/runtime/protoimpl#EnforceVersion.

This PR already forces a compile break for the Name() → NameExpr() API change.

@stephenafamo stephenafamo merged commit 79bbaba into stephenafamo:main May 22, 2026
8 checks passed
@atzedus atzedus deleted the feat/view-name-expr branch May 25, 2026 08:30
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