I'm using GORM for mapping MySQL DB tables which may have extra columns that are not part of the GORM models for them.
Selecting any row form such a table crashes because GORM emits a SELECT * FROM ... query and tries to scan the results in the model struct which is obviously impossible because there are more returned columns than expected.
Is there a way to force GORM to explicitly name the columns in the emitted SELECT queries?
Please do skim through docs next time
From the 1.4.2 Query chapter:
Specify fields that you want to retrieve from database, by default, will select all fields;
db.Select("name, age").Find(&users)
//// SELECT name, age FROM users;
db.Select([]string{"name", "age"}).Find(&users)
//// SELECT name, age FROM users;
db.Table("users").Select("COALESCE(age,?)", 42).Rows()
//// SELECT COALESCE(age,'42') FROM users;
Please do skim through docs next time
From the 1.4.2 Query chapter:
Specify fields that you want to retrieve from database, by default, will select all fields; db.Select("name, age").Find(&users) //// SELECT name, age FROM users; db.Select([]string{"name", "age"}).Find(&users) //// SELECT name, age FROM users; db.Table("users").Select("COALESCE(age,?)", 42).Rows() //// SELECT COALESCE(age,'42') FROM users;
@zardak My table has a lot of columns. Is there a Ignore function to ignore some columns?
Most helpful comment
Please do skim through docs next time
From the 1.4.2 Query chapter: