0

I am trying to query data from SQL Server using Golang and GORM. But I am getting below error:-

DB Connection: &{{{0 0} 0 0 0 0} <nil> <nil> 0 0xc0000a4480 false 0 {0xc0000e5db0} <nil> {{0 0} {<nil>} map[] 0} 0xc00004c5b0 0xaccbe0 0xc00014d0a0 false <nil>}

HasTable- Currency: true
{CurrencyId:0 Code: Description:}

[35m(C:/Users/RahulBFL/Documents/architechbc/dot net/GORM/main.go:26)[0m
[33m[2019-12-27 15:56:13][0m [31;1m mssql: Invalid object name 'currencies'. [0m

My Table Schema looks like below:-

     Currency
CurrencyId    int
Code          char
Description   varchar

On SSMS, I am successfully able to query the table using the below command

select * from IMBookingApp.dbo.Currency

My Golang code

package main

import (
    "fmt"
    "github.com/jinzhu/gorm"
    _ "github.com/jinzhu/gorm/dialects/mssql"
)

type Currency struct {
    CurrencyId  int
    Code        string
    Description string
}

func main() {

    db, err := gorm.Open("mssql", "sqlserver://USERNAME:PASSWORD@Endpoint:1433?database=DATABASENAME&Initial+Catalog=SCHEMA_NAME")

    if err != nil {
        fmt.Println("Connection Error:", err)
    }

   fmt.Println("DB Connection", db)


    fmt.Println("HasTable- Currency:", db.HasTable("ClientUser"))

    var Currency Currency
    db.Find(&Currency)

    fmt.Printf("%+v\n", Currency)

    defer db.Close()
}

I don't understand why I am getting the error as - Invalid object name 'currencies'. Though there is no currencies in the schema, there is only currency.

Any help will be appreciated.

1 Answer 1

2

If you check your error Invalid object name 'currencies' it tries to find table named currencies. By default gorm pluralizes tables names. If you want a custom table name you can define it like below :

func (Currency) TableName() string {
    return "currency"
}

Or you can globally disable it with :

db.SingularTable(true)

So you sample should look like this :

type Currency struct {
    CurrencyId  int
    Code        string
    Description string
    CreateDate  time.Time
}

func (Currency) TableName() string {
    return "currency"
}


func main() {
    db, err := gorm.Open("mssql", "sqls*******talog=dbo")

    db.SingularTable(true)

    fmt.Println("db.HasTable: Currency:", db.HasTable("ClientUser"))

    var Currency []Currency 

    db.Find(&Currency)

    ....
}

Sign up to request clarification or add additional context in comments.

12 Comments

Thanks for your response. After adding db.SingularTable(true), this error is resolved - Invalid object name 'currencies'. However, I am not getting any rows in the response.
@RahulSatal find may expect an array to be passed. Find will find all objects in the table. You can try using first method or pass an array of Currency
After using the function TableName. I am getting another error - unsupported destination, should be slice or struct. I may not be using the function correctly. You can see my code here - play.golang.org/p/g9fwRcq-pQw
Thanks for the code. However, from the above code I am getting empty string with no error as - [{CurrencyId:0 Code: Description: CreateDate:0001-01-01 00:00:00 +0000 UTC}]
Also, with var Currency Currency and db.First(&Currency) . I am getting this error - mssql: Invalid usage of the option NEXT in the FETCH statement.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.