Closed Gys closed 1 year ago
Hi @Gys,
Your query does not match the destination type. Check the wiki.
There are two ways you can fix your code, you can change destination type to TableBug
or you use autogenerated type model.TableBug
Hi @go-jet! Thanks for the quick response.
From the wiki I understand the autogenerated type can be a destination type. You say the same in your comment?
In my example code the model_bug.go is a renamed copy of the autogenerated file model/bug.go. I only changed the filename (model/bug.go -> model_bug.go), package name (model -> bug) and type name to prevent name conflicts (type Bug -> type ModelBug).
I made the same adaption for table/bug.go -> table_bug.go
So I am using the autogenerated model.TableBug (actually, the generated code is model.Bug) by using ModelBug. But maybe I made some (other) mistake?
I thought copying model/bug.go to model_bug.go would not matter. But I now find that referring to model.Bug instead of ModelBug indeed makes a difference. Strange. Anyways, the test works in that case.
Different package doesn't make any difference, only struct type name and struct field name matters. The easiest way for you to spot a mistake, if you're using custom destination types, is by printing the statement. For instance:
selectQ1 := SELECT(TableBug.AllColumns).FROM(TableBug)
fmt.Println(selectQ1.Sql())
Will give print query:
SELECT table_bug.id as "table_bug.id" -- aliasing is specifing destination
table_bug.title as "table_bug.title"
FROM table_bug;
Now you know that destination has to be TableBug
struct with two fields ID
and title
.
Thank you for explaining very clearly!
Different package doesn't make any difference, only struct type name and struct field name matters. The easiest way for you to spot a mistake, if you're using custom destination types, is by printing the statement. For instance:
selectQ1 := SELECT(TableBug.AllColumns).FROM(TableBug) fmt.Println(selectQ1.Sql())
Will give print query:
SELECT table_bug.id as "table_bug.id" -- aliasing is specifing destination table_bug.title as "table_bug.title" FROM table_bug;
Now you know that destination has to be
TableBug
struct with two fieldsID
andtitle
.
Regarding this, I have a question: Is there a global configuration that can directly remove the table name prefix from all fields? For example:
SELECT table_bug.id as "id" -- aliasing is specifing destination
table_bug.title as "title"
FROM table_bug;
No there is no such config. Although on master branch there is As
method on ColumnList
type, but it is still per query:
SELECT(
TableBug.AllColumns.As(""), // this will remove table name from alias.
).FROM(
TableBug,
)
No there is no such config. Although on master branch there is
As
method onColumnList
type, but it is still per query:SELECT( TableBug.AllColumns.As(""), // this will remove table name from alias. ).FROM( TableBug, )
From what I see now, I think it's enough. However, I still hope to be able to specify when generating. Moreover, I initially thought that marking alias:"xxx"
on the struct could solve this issue. My question is, what problem does alias:"xxx"
solve?
It is mostly used for custom model types to match fields with projection. You can find more info here: https://github.com/go-jet/jet/wiki/Query-Result-Mapping-(QRM).
I'm not sure what are you trying to achieve. Are you using jet as struct mapper? Are you using custom model types?
Describe the bug My table has one record. Doing a SELECT(Table.AllColumns).FROM(Table) does not return the one record.
Environment
Code snippet
See bug_test.go below with other required files. Expected behavior: all tests in TestBug() should pass, meaning the one record should be found and the found ID should be the same.
sqlite3 bug.sqlite 'select * from bug;'
shows the table contents.FILE: bug_test.go
FILE: create_bug_table.go
FILE: model_bug.go
FILE: table_bug.go