Skip to content

Question about aliasing #532

@yurivish

Description

@yurivish

Describe the bug
Hello! Thanks for this great package. I've been playing around with it and love the way it allows type-safe but dynamic query construction. However, I've run into a puzzling corner case I was hoping you could help with.

The wiki page on QRM says that you should be able to map a result to a struct purely based on the .AS("Name") corresponding to the same-named Name field in a result struct.

At least, that's the idea I got from this example in the wiki.

But in the query below, this does not work, and I get an error about an unused column, since I turned on strict scan.

So, using Experiment.ExpID.AS("Name") in my SELECT condition fails with an error, but using the fully-qualified Experiment.ExpID.AS("ResultRow.Name") works, even though according to the docs it seems that both should work.

Any ideas? I really like the short form because it makes my query code a lot easier to read by reducing the boilerplate for queries that select lots of fields at once.

FWIW I also tried declaring the field with alias:"Name", alias:"name", and a few other variants, none of which seemed to do the trick.

Thanks again!

Environment (please complete the following information):

  • OS: macOS
  • Database: SQLite
  • Database driver: modernc/sqlite
  • Jet version v2 2.14

Code snippet

type ResultRow struct {
	Name string
}

func ListExperiments(db *sql.DB, where BoolExpression) ([]ResultRow, error) {
	stmt := SELECT(
		Experiment.ExpID.AS("Name"),
	).FROM(Experiment)
	var dest []ResultRow
	err := stmt.Query(db, &dest)
	return dest, err
}
var Experiment = newExperimentTable("", "experiment", "")

type experimentTable struct {
	sqlite.Table

	// Columns
	ID         sqlite.ColumnInteger
	CtxID      sqlite.ColumnString
	ExpID      sqlite.ColumnString
	Dir        sqlite.ColumnString
	Experiment sqlite.ColumnBlob
	Results    sqlite.ColumnBlob

	AllColumns     sqlite.ColumnList
	MutableColumns sqlite.ColumnList
	DefaultColumns sqlite.ColumnList
}

Expected behavior
I would expect a result set, but get an error.

2025/11/26 16:10:33 http: panic serving [::1]:60135: jet: columns never used: 'Name'
goroutine 39 [running]:
net/http.(*conn).serve.func1()
	/usr/local/go/src/net/http/server.go:1943 +0xb4
panic({0x10291dba0?, 0x14000520650?})
	/usr/local/go/src/runtime/panic.go:783 +0x120
github.com/go-jet/jet/v2/qrm.(*ScanContext).EnsureEveryColumnRead(0x14000530480)
	/Users/yurivish/go/pkg/mod/github.com/go-jet/jet/[email protected]/qrm/scan_context.go:78 +0x1d8
github.com/go-jet/jet/v2/qrm.queryToSlice({0x1029d9f88?, 0x102df4280?}, {0x1029d61a8?, 0x140002bc820?}, {0x1400052e740?, 0x10255b42c?}, {0x0?, 0x1027e2e2e?, 0x10053f4b8?}, {0x102914cc0, ...})
	/Users/yurivish/go/pkg/mod/github.com/go-jet/jet/[email protected]/qrm/qrm.go:277 +0x200
github.com/go-jet/jet/v2/qrm.Query({0x1029d9f88, 0x102df4280}, {0x1029d61a8, 0x140002bc820}, {0x1400052e740, 0x35}, {0x0, 0x0, 0x0}, {0x102914cc0, ...})
	/Users/yurivish/go/pkg/mod/github.com/go-jet/jet/[email protected]/qrm/qrm.go:171 +0x120
github.com/go-jet/jet/v2/internal/jet.(*statementInterfaceImpl).Query.(*statementInterfaceImpl).QueryContext.func1({0x1400052e740?, 0x2c2c0390?}, {0x0?, 0x69276cc9?, 0x1400053f5b8?})
	/Users/yurivish/go/pkg/mod/github.com/go-jet/jet/[email protected]/internal/jet/statement.go:114 +0x160
github.com/go-jet/jet/v2/internal/jet.(*statementInterfaceImpl).query.func1()
	/Users/yurivish/go/pkg/mod/github.com/go-jet/jet/[email protected]/internal/jet/statement.go:131 +0x3c
github.com/go-jet/jet/v2/internal/jet.duration(0x1400053f6d0)
	/Users/yurivish/go/pkg/mod/github.com/go-jet/jet/[email protected]/internal/jet/statement.go:210 +0x38
github.com/go-jet/jet/v2/internal/jet.(*statementInterfaceImpl).query(0x1400051a250, {0x1029d9f88, 0x102df4280}, 0x1400053f748)
	/Users/yurivish/go/pkg/mod/github.com/go-jet/jet/[email protected]/internal/jet/statement.go:130 +0xc0
github.com/go-jet/jet/v2/internal/jet.(*statementInterfaceImpl).QueryContext(...)
	/Users/yurivish/go/pkg/mod/github.com/go-jet/jet/[email protected]/internal/jet/statement.go:107
github.com/go-jet/jet/v2/internal/jet.(*statementInterfaceImpl).Query(0x140005107c8?, {0x1029d61a8?, 0x140002bc820?}, {0x102914cc0?, 0x140005180c0?})
	/Users/yurivish/go/pkg/mod/github.com/go-jet/jet/[email protected]/internal/jet/statement.go:103 +0x60
main.ListExperiments(0x140002bc820, {0x1029e2250?, 0x14000522440?})
	/Users/yurivish/Dropbox/Projects/IOP/llmb/queries.go:44 +0x15c
main.(*App).contextHandler(0x140000b6840, {0x1029d9320, 0x1400051cb10}, 0x10308c3c8?)
	/Users/yurivish/Dropbox/Projects/IOP/llmb/main.go:99 +0x198
net/http.HandlerFunc.ServeHTTP(0x140000de000?, {0x1029d9320?, 0x1400051cb10?}, 0x1027a1a60?)
	/usr/local/go/src/net/http/server.go:2322 +0x38
net/http.(*ServeMux).ServeHTTP(0x1400052c500?, {0x1029d9320, 0x1400051cb10}, 0x1400052c640)
	/usr/local/go/src/net/http/server.go:2861 +0x190
main.(*App).Serve.(*SessionManager).LoadAndSave.func1({0x1029d94d0, 0x140005522d0}, 0x1400052c500)
	/Users/yurivish/go/pkg/mod/github.com/alexedwards/scs/[email protected]/session.go:161 +0x138
net/http.HandlerFunc.ServeHTTP(0x140002af5f0?, {0x1029d94d0?, 0x140005522d0?}, 0x102333bd8?)
	/usr/local/go/src/net/http/server.go:2322 +0x38
main.(*App).Serve.(*CrossOriginProtection).Handler.func2({0x1029d94d0, 0x140005522d0}, 0x1400052c500)
	/usr/local/go/src/net/http/csrf.go:212 +0xdc
net/http.HandlerFunc.ServeHTTP(0x1400052a280?, {0x1029d94d0?, 0x140005522d0?}, 0x14000510b50?)
	/usr/local/go/src/net/http/server.go:2322 +0x38
net/http.serverHandler.ServeHTTP({0x14000522340?}, {0x1029d94d0?, 0x140005522d0?}, 0x6?)
	/usr/local/go/src/net/http/server.go:3340 +0xb0
net/http.(*conn).serve(0x14000530360, {0x1029da248, 0x140002af860})
	/usr/local/go/src/net/http/server.go:2109 +0x528
created by net/http.(*Server).Serve in goroutine 1
	/usr/local/go/src/net/http/server.go:3493 +0x384
2025/11/26 16:10:38 http: panic serving [::1]:60137: jet: columns never used: 'Name'
goroutine 41 [running]:
net/http.(*conn).serve.func1()
	/usr/local/go/src/net/http/server.go:1943 +0xb4
panic({0x10291dba0?, 0x14000592130?})
	/usr/local/go/src/runtime/panic.go:783 +0x120
github.com/go-jet/jet/v2/qrm.(*ScanContext).EnsureEveryColumnRead(0x140005b8000)
	/Users/yurivish/go/pkg/mod/github.com/go-jet/jet/[email protected]/qrm/scan_context.go:78 +0x1d8
github.com/go-jet/jet/v2/qrm.queryToSlice({0x1029d9f88?, 0x102df4280?}, {0x1029d61a8?, 0x140002bc820?}, {0x140005b2080?, 0x10255b42c?}, {0x0?, 0x1027e2e2e?, 0x1005ab4b8?}, {0x102914cc0, ...})
	/Users/yurivish/go/pkg/mod/github.com/go-jet/jet/[email protected]/qrm/qrm.go:277 +0x200
github.com/go-jet/jet/v2/qrm.Query({0x1029d9f88, 0x102df4280}, {0x1029d61a8, 0x140002bc820}, {0x140005b2080, 0x35}, {0x0, 0x0, 0x0}, {0x102914cc0, ...})
	/Users/yurivish/go/pkg/mod/github.com/go-jet/jet/[email protected]/qrm/qrm.go:171 +0x120
github.com/go-jet/jet/v2/internal/jet.(*statementInterfaceImpl).Query.(*statementInterfaceImpl).QueryContext.func1({0x140005b2080?, 0x2cf742d0?}, {0x0?, 0x69276cce?, 0x140005ab5b8?})
	/Users/yurivish/go/pkg/mod/github.com/go-jet/jet/[email protected]/internal/jet/statement.go:114 +0x160
github.com/go-jet/jet/v2/internal/jet.(*statementInterfaceImpl).query.func1()
	/Users/yurivish/go/pkg/mod/github.com/go-jet/jet/[email protected]/internal/jet/statement.go:131 +0x3c
github.com/go-jet/jet/v2/internal/jet.duration(0x140005ab6d0)
	/Users/yurivish/go/pkg/mod/github.com/go-jet/jet/[email protected]/internal/jet/statement.go:210 +0x38
github.com/go-jet/jet/v2/internal/jet.(*statementInterfaceImpl).query(0x140005a2010, {0x1029d9f88, 0x102df4280}, 0x140005ab748)
	/Users/yurivish/go/pkg/mod/github.com/go-jet/jet/[email protected]/internal/jet/statement.go:130 +0xc0
github.com/go-jet/jet/v2/internal/jet.(*statementInterfaceImpl).QueryContext(...)
	/Users/yurivish/go/pkg/mod/github.com/go-jet/jet/[email protected]/internal/jet/statement.go:107
github.com/go-jet/jet/v2/internal/jet.(*statementInterfaceImpl).Query(0x140000a07c8?, {0x1029d61a8?, 0x140002bc820?}, {0x102914cc0?, 0x140005a4000?})
	/Users/yurivish/go/pkg/mod/github.com/go-jet/jet/[email protected]/internal/jet/statement.go:103 +0x60
main.ListExperiments(0x140002bc820, {0x1029e2250?, 0x14000596040?})
	/Users/yurivish/Dropbox/Projects/IOP/llmb/queries.go:44 +0x15c
main.(*App).contextHandler(0x140000b6840, {0x1029d9320, 0x14000594060}, 0x149be21c8?)
	/Users/yurivish/Dropbox/Projects/IOP/llmb/main.go:99 +0x198
net/http.HandlerFunc.ServeHTTP(0x140000de000?, {0x1029d9320?, 0x14000594060?}, 0x1027a1a60?)
	/usr/local/go/src/net/http/server.go:2322 +0x38
net/http.(*ServeMux).ServeHTTP(0x1400030e000?, {0x1029d9320, 0x14000594060}, 0x14000598000)
	/usr/local/go/src/net/http/server.go:2861 +0x190
main.(*App).Serve.(*SessionManager).LoadAndSave.func1({0x1029d94d0, 0x14000338000}, 0x1400030e000)
	/Users/yurivish/go/pkg/mod/github.com/alexedwards/scs/[email protected]/session.go:161 +0x138
net/http.HandlerFunc.ServeHTTP(0x140002af5f0?, {0x1029d94d0?, 0x14000338000?}, 0x102333bd8?)
	/usr/local/go/src/net/http/server.go:2322 +0x38
main.(*App).Serve.(*CrossOriginProtection).Handler.func2({0x1029d94d0, 0x14000338000}, 0x1400030e000)
	/usr/local/go/src/net/http/csrf.go:212 +0xdc
net/http.HandlerFunc.ServeHTTP(0x14000306020?, {0x1029d94d0?, 0x14000338000?}, 0x140000a0b50?)
	/usr/local/go/src/net/http/server.go:2322 +0x38
net/http.serverHandler.ServeHTTP({0x1400030c000?}, {0x1029d94d0?, 0x14000338000?}, 0x6?)
	/usr/local/go/src/net/http/server.go:3340 +0xb0
net/http.(*conn).serve(0x14000530510, {0x1029da248, 0x140002af860})
	/usr/local/go/src/net/http/server.go:2109 +0x528
created by net/http.(*Server).Serve in goroutine 1
	/usr/local/go/src/net/http/server.go:3493 +0x384

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions