feat: support generated (computed) columns via the generated tag#178
Open
h2zi wants to merge 2 commits into
Open
feat: support generated (computed) columns via the generated tag#178h2zi wants to merge 2 commits into
generated tag#178h2zi wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
Adds SQL Server support for GORM’s generated (computed) columns by recognizing a generated tag and emitting SQL Server’s computed-column clause (AS (<expr>) PERSISTED) instead of a normal data type, aligning behavior with generated-column support in other GORM drivers.
Changes:
- Update
Dialector.DataTypeOfto returnAS (<expr>) PERSISTEDwhen theGENERATEDtag is present and not reserved for identity. - Add parsing helpers to extract/validate the computed expression and avoid treating
identityas a computed expression. - Add a unit test covering computed clause rendering, comma-safe expressions, and
identitykeyword handling.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| sqlserver.go | Detects generated tag and emits SQL Server computed-column DDL (AS (...) PERSISTED), with helpers to avoid identity conflicts. |
| generated_test.go | Adds unit coverage for computed column rendering and identity keyword handling. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Render SQL Server computed columns from a `generated` tag, keeping the generation expression separate from the column type. SQL Server infers a computed column's type from its expression, so the type is omitted and the value is PERSISTED (stored): Total float64 `gorm:"->;generated:price * quantity"` // -> [total] AS (price * quantity) PERSISTED The expression is taken verbatim, so commas inside it (e.g. concat(a, b)) are preserved; combine with the `->` read-only permission. The `identity` keyword is reserved for identity columns and is rendered through SQL Server's native IDENTITY, so it is not treated as a computed-column expression. Relates to go-gorm/gorm#7191 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
The module requires `go 1.24.0`, but the workflow matrix still pinned Go
1.19–1.21, which cannot parse the go.mod ("invalid go version '1.24.0': must
match format 1.23") and failed every run before building. Use oldstable/stable,
matching the other GORM driver repositories.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
8e4d774 to
9516e9e
Compare
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.
What this adds
Support for SQL Server computed columns through the
generatedstruct tag, keeping the generation expression separate from the column type. Part of rounding out generated-column support across the GORM drivers (see go-gorm/postgres#345, go-gorm/sqlite#230, go-gorm/mysql#189). Relates to go-gorm/gorm#7191.generatedvalue is taken verbatim as the expression of a computed column. SQL Server infers the column type from the expression, so the type is omitted, andPERSISTEDstores the value (matching theSTOREDsemantics used by the other drivers). Commas inside the expression are preserved, sogenerated:concat(a, b)works.->so GORM treats the column as read-only (SQL Server forbids writing computed columns).identitykeyword is reserved for identity columns and is left to the existingIDENTITY(1,1)handling rather than being mistaken for an expression.Implementation
DataTypeOfreturnsAS (<expr>) PERSISTEDwhen a computed expression is present; the base column type is computed exactly as before otherwise.Tests
Unit test
TestDataTypeOfGeneratedColumncovers thePERSISTEDcomputed clause, comma-safe expressions, and the reservedidentitykeyword. The full suite (including integration against the real SQL Server service) passes in CI.Also: CI fix (separate commit)
The workflow matrix still pinned Go 1.19–1.21 while
go.modrequiresgo 1.24.0, so every run — including onmaster— failed before building (invalid go version '1.24.0': must match format 1.23). A second commit switches the matrix tooldstable/stable, matching the other GORM driver repos, which is what makes the suite above actually runnable again. Happy to split this into its own PR if preferred.🤖 Generated with Claude Code