From 9d4a1eab10ce64d0fb41d6342a725835fe9cb724 Mon Sep 17 00:00:00 2001 From: ~wispem-wantex Date: Sat, 19 Sep 2026 21:36:31 -0700 Subject: [PATCH] fix (codegen): don't duplicate the primary-key getter for 'without rowid' tables --- cmd/subcmd_generate_models.go | 4 ++++ pkg/schema/index.go | 11 ++++++++++- pkg/schema/parse.go | 2 +- pkg/schema/views.sql | 3 ++- 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/cmd/subcmd_generate_models.go b/cmd/subcmd_generate_models.go index 3fd1d95..b1ced39 100644 --- a/cmd/subcmd_generate_models.go +++ b/cmd/subcmd_generate_models.go @@ -87,6 +87,10 @@ var generate_model = &cobra.Command{ // Skip expression indexes; there's no way to resolve an expression to a real column continue } + if index.Origin == "pk" && table.IsWithoutRowid { + // Already generated above, as the primary-key getter + continue + } cols := make([]schema.Column, len(index.Columns)) for i, colName := range index.Columns { cols[i] = table.GetColumnByName(colName) diff --git a/pkg/schema/index.go b/pkg/schema/index.go index 2d4863f..898890e 100644 --- a/pkg/schema/index.go +++ b/pkg/schema/index.go @@ -1,10 +1,19 @@ package schema +type IndexOrigin string + +const ( + IndexOriginCreateIndex = IndexOrigin("c") + IndexOriginUniqueConstraint = IndexOrigin("u") + IndexOriginPrimaryKey = IndexOrigin("pk") +) + type Index struct { Name string `db:"index_name"` TableName string `db:"table_name"` Columns []string - IsUnique bool `db:"is_unique"` + IsUnique bool `db:"is_unique"` + Origin IndexOrigin `db:"origin"` // TODO: `where ...` for partial indexes // TODO: identify columns that are expressions } diff --git a/pkg/schema/parse.go b/pkg/schema/parse.go index 1393ad7..ac0c5a1 100644 --- a/pkg/schema/parse.go +++ b/pkg/schema/parse.go @@ -49,7 +49,7 @@ func SchemaFromDB(db *sqlx.DB) Schema { } var indexes []Index - must.Do(db.Select(&indexes, `select index_name, table_name, is_unique from indexes`)) + must.Do(db.Select(&indexes, `select index_name, table_name, is_unique, origin from indexes`)) for _, idx := range indexes { must.Do(db.Select(&idx.Columns, `select column_name from index_columns where index_name = ? order by rank`, idx.Name)) ret.Indexes[idx.Name] = idx diff --git a/pkg/schema/views.sql b/pkg/schema/views.sql index 2b6825a..211f2e0 100644 --- a/pkg/schema/views.sql +++ b/pkg/schema/views.sql @@ -27,7 +27,8 @@ create temporary view columns as create temporary view indexes as select idx.name as index_name, tables.name as table_name, - idx."unique" as is_unique + idx."unique" as is_unique, + idx.origin as origin from tables join pragma_index_list(tables.name) idx;