Repository navigation
fix(seedling-gen): Generate usable defaults for conventional GORM models - #101
Merged
Merged
Conversation
When no field carries a primaryKey tag, GORM treats the field named ID as the primary key, and the generator already registered it as the blueprint key. The defaults still saw the field as an ordinary uint and set it to 1, so every insert after the first failed with a unique constraint violation. The defaults now exclude every key field the blueprint registers, whether tagged or inferred.
Models embedding gorm.Model had their promoted CreatedAt and UpdatedAt
defaults written as top-level keys of the model literal, which Go
rejects for promoted fields, so the generated file did not compile.
The normalized IR now records the embedded struct that promotes each
field, and the defaults literal groups those assignments under it as
Model: gorm.Model{...}. A hand-written fixture with an untagged ID and
an embedded gorm.Model runs repeated and batch inserts against SQLite.
Each adapter added the time import whenever any model field had type time.Time, even when that field was a key or relation field the defaults skip, so a model whose only time.Time field was its primary key generated code that failed with "time" imported and not used. The shared renderer now decides the import itself: emitted struct field types count only for the SQL and Atlas adapters that write structs, and otherwise only a rendered time default does.
…faults Note that generated defaults skip GORM's untagged ID key, write fields promoted from gorm.Model through the embedded struct literal, and add the time import only when generated code uses it.
Code Metrics Report
Details | | main (4880ed2) | #101 (d5a0588) | +/- |
|---------------------|----------------|----------------|-------|
+ | Coverage | 86.7% | 86.8% | +0.1% |
| Files | 59 | 59 | 0 |
| Lines | 4771 | 4790 | +19 |
+ | Covered | 4139 | 4161 | +22 |
+ | Code to Test Ratio | 1:0.9 | 1:1.0 | +0.0 |
| Code | 17736 | 17783 | +47 |
+ | Test | 17596 | 17797 | +201 |
- | Test Execution Time | 48s | 1m18s | +30s |Code coverage of files in pull request scope (87.6% → 88.1%, patch 96.4%)
Reported by octocov |
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.
Summary
IDprimary key to the database so repeated inserts no longer collide onID: 1gorm.Modelby nesting promoted fields under the embedded structtimeimport only when the generated code actually references itChanges
IDfield GORM treats as the primary key when no field carries aprimaryKeytagModel: gorm.Model{...}instead of invalid top-level promoted keysIDand an embeddedgorm.Modelis migrated into SQLite and exercised with repeated inserts, batch inserts, and cleanuptime.Timedefault doestime.Timefield is a key now compiles instead of failing with an unused importgorm.Modeldefaults, and conditionaltimeimport