Closed zikaeroh closed 5 years ago
I appreciate the concern. I didn't think this through too much. I do think a mini-reversion is necessary. This feature will either be hidden behind a flag, or more hopefully, information schema will have an ordinal type column we can use to derive consistent order from there and not have to rely on alpha sorting.
This should order the columns in the way you expect as well as give us idempotency now. Done as I suggested (ordinal index in information_schema). Thanks for reporting the issue.
Thank you!
If you're having a generation problem please answer these questions before submitting your issue. Thanks!
What version of SQLBoiler are you using (
sqlboiler --version
)?SQLBoiler v3.3.0
If this happened at generation time what was the full SQLBoiler command you used to generate your models? (if not applicable leave blank)
(I use gobin plus a script which execs gobin again for sqlboiler-psql, pinning all my deps in go.mod; this isn't important to this issue.)
If this happened at runtime what code produced the issue? (if not applicable leave blank)
-
What is the output of the command above with the
-d
flag added to it? (Provided you are comfortable sharing this, it contains a blueprint of your schema)Don't think this is needed.
Please provide a relevant database schema so we can replicate your issue (Provided you are comfortable sharing this)
Further information. What did you do, what did you expect?
I would have expected my generated Go types to have the same field order between SQLBoiler versions.
PR #475 made generation "idempotent". While probably a good change for table ordering, since columns are now sorted there are a few issues:
Types generated by 3.3.0 are technically not backwards compatible. Since the struct field ordering has changed, if you haven't explicitly named the fields in a literal, then the order changing means that existing code will break when the models are regenerated (e.g.
{123, "foo"}
might be okay before, but maybe not after). Linters heavily warn against omitting the names for exactly this reason, so this isn't a big deal. However...The generated structs are a lot less "nice" looking. For my schema above, 3.2.0 generates the type:
The ID field comes first, and the ordering is what I wrote in my schema. But 3.3.0 generates:
As it's sorted, this ordering has nothing to do with how my table is constructed. The ID field is now somewhere in the middle (which is weird). I usually write my schemas in somewhat of an order of importance (before migrations change things up), so having this order changed on me doesn't feel very good.
I haven't yet seen a case where modifying a table changes the orders of the columns, which seems to have been a motivating factor of #475. The order in which the types and their methods are defined in the final output doesn't matter; godoc will sort them out, but the fields themselves are a core part of using the models and I believe take a bit more care.
Can the sorting of fields be optional so I can retain the old behavior? (Or maybe some solution in the middle where they'll be reordered back before being written?)