Gorm: How to use explicit column names in SELECT queries?

Created on 10 Nov 2016  路  2Comments  路  Source: go-gorm/gorm

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?

Most helpful comment

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;

All 2 comments

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?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

satb picture satb  路  3Comments

Quentin-M picture Quentin-M  路  3Comments

sredxny picture sredxny  路  3Comments

bramp picture bramp  路  3Comments

Ganitzsh picture Ganitzsh  路  3Comments