feat(dialect): add NameExpr/NameAsExpr and fix schema column qualifiers#692
Merged
Merged
Conversation
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
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. |
Contributor
Author
|
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
View/TableName()now returns the bare table or view name as astring. SQL builders should useNameExpr()(formerlyName()) andNameAsExpr()(formerlyNameAs()).Schema()on PostgreSQL and SQLiteViewtypes (MySQL is unchanged — no schema/database qualification on models).tableColumnAliasin codegen sobuild*Columns,pkEQ, andpkINuse the same qualifier asView.Alias()whenschemais set at construction (schema.tableinstead of a baretable.Keysuch asusers).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.idwhen the model is built with a schema.Assume the same table everywhere:
usersidmodels.UsersOnly the schema at construction differs.
Case A —
shared_schema(schema empty at codegen)Codegen emits:
Runtime:
Users.Schema()""Users.Name()"users"Users.Alias()"users"Users.Columns.ID"users"."id"Users.NameAsExpr()"users" AS "users"Manual query:
Prefix
usersis consistent everywhere. No bug —table.Key("users") matchesAlias().Case B — non-shared schema (schema
billingat codegen)Codegen emits:
Runtime (dialect):
Users.Schema()"billing"Users.Name()"users"Users.Alias()"billing.users"Users.NameAsExpr()"billing"."users" AS "billing.users"Users.Query()columns (viaView.ColumnsExpr)"billing.users"."id"Before this PR — codegen passed
table.Key("users") intobuildUsersColumnsandpkEQ:buildUsersColumns("users")"users"."id"pkEQ()Quote("users", "id")Manual query with the same
models.Usersand sameid:Users.Query()alone looked fine (it usesView.ColumnsExprwith parent"billing.users"), butUsers.Columns.IDandpkEQ()pointed at"users"."id"while the SQL alias was"billing.users".After this PR —
tableColumnAlias("billing", "users", "users")→"billing.users":Users.Columns.ID"billing.users"."id"pkEQ()Quote("billing.users", "id")Same manual query:
Same table
users, same columnid— only the schema argument changes the prefix, and codegen now matchesView.Alias().Migration
Users.Name()insm.From, joins, etc.Users.NameExpr()Users.NameAs()Users.NameAsExpr()ExpressionNameExpr()/NameAsExpr()Users.Name()Users.Schema()Regenerate models after upgrading bobgen.