feat: support generated (computed) columns via the generated tag#230
Open
h2zi wants to merge 1 commit into
Open
feat: support generated (computed) columns via the generated tag#230h2zi wants to merge 1 commit into
generated tag#230h2zi wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
Adds SQLite support in the GORM SQLite dialector for STORED generated (computed) columns via a generated struct tag by appending the GENERATED ALWAYS AS (...) STORED clause to the column’s SQL type during migration.
Changes:
- Extend
Dialector.DataTypeOfto emit... GENERATED ALWAYS AS (<expr>) STOREDwhen aGENERATEDtag setting is present. - Add parsing helpers to treat
identity(optionally withalways/by default) as a reserved keyword (i.e., not a computed expression). - Add unit test coverage for the generated-column datatype rendering behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| sqlite.go | Adds generated-column handling in DataTypeOf and helpers to interpret the generated tag and reserve identity. |
| generated_test.go | Adds unit tests validating generated-column datatype rendering (including comma-containing expressions and reserved identity). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
3a85a16 to
94d4368
Compare
Render SQLite STORED generated columns from a `generated` tag, keeping the generation expression separate from the column type: Total float64 `gorm:"->;generated:price * quantity"` // -> "total" real GENERATED ALWAYS AS (price * quantity) STORED The expression is taken verbatim, so commas inside it (e.g. coalesce(a, b)) are preserved; combine with the `->` read-only permission. The `identity` keyword is reserved for identity columns and is rendered through SQLite's native AUTOINCREMENT, 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>
94d4368 to
13d7cf0
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 SQLite STORED generated 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 for the PostgreSQL side, which also covers identity columns). Relates to go-gorm/gorm#7191.generatedvalue is taken verbatim as the expression of aSTOREDgenerated column (SQLite 3.31+). Commas inside the expression are preserved, sogenerated:coalesce(a, b)works.->so GORM treats the column as read-only (SQLite forbids writing generated columns).identitykeyword is reserved for identity columns; SQLite has no SQL-standard identity, sogenerated:identityis left to the existingAUTOINCREMENThandling rather than being mistaken for an expression.Implementation
DataTypeOfappends theGENERATED ALWAYS AS (...) STOREDclause when a computed expression is present; the base column type is computed exactly as before.Tests & verification
TestDataTypeOfGeneratedColumncovers the computed clause, comma-safe expressions, and the reservedidentitykeyword.CREATE TABLEemits"total" real GENERATED ALWAYS AS (price * quantity) STORED,INSERTomits the generated column, the read-back value is correct, and repeatedAutoMigrateruns are idempotent.🤖 Generated with Claude Code