Skip to content
Advertisement

In go – gorm “mssql: Invalid column name ‘id’

This is my model in go

}

and this is my repo(that retrieve data from database):

}

On “http” I write this code with echo

When I cal my ReadAll record rerieve data completly , it’s mean I conected to database currectly,But whene I try get data by “id” got an error that :

mssql: Invalid column name ‘id’.

Whet is my mistake another hand make id in my struct as a comment like this:>

got new error that

mssql: Invalid column name ‘name’. [2690.814ms] [rows:0] SELECT * FROM “CompanyNames” WHERE “CompanyNames”.”name” = ‘1’ ORDER BY “CompanyNames”.”id” OFFSET 0 ROW FETCH NEXT 1 ROWS ONLY

Update:

func Cunnection() (*gorm.DB, error) { var ( err error db *gorm.DB ) dsn := “sqlserver://DbUser:mypass@111.111.111.111:1433?database=myDb”

}

Advertisement

Answer

After reading your own answer, I found your question. As the comments above say, it’s easier to find the problem if you show your table structure.

The column name specified by your query is Id and your structure does not explicitly specify this name. According to the Grom convention, your Id field will be converted into id. This problem can be solved by adding tags.

For example:

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement