我正在尝试使用GORM连接到Microsoft SQL服务器:https://github.com/jinzhu/gorm
但是当我尝试使用db.HasTable()时,我似乎无法找到任何表格,并且我检查了正常的凭据。我确实收到一条消息,GORM没有正式支持MSSQL,它在兼容模式下运行,但我还包括一个SQL驱动程序:github.com/denisenkom/go-mssqldb 那用于MSSQL。有什么我想念的吗?
答案 0 :(得分:3)
我发现了我的错误,我导入了错误的MSSQL驱动程序,gorm已经有了一个 import _“github.com/jinzhu/gorm/dialects/mssql”
答案 1 :(得分:0)
我将发布这个答案只是为了在上面解释@Eduardo Carstillo的答案。
我有同样的问题,我得到了错误
mssql is not officially supported, running under compatibility mode.
发生错误是因为Gorm库默认情况下不支持mssql,但是它已在gorm dialects
中实现。
要解决此问题。只需为gorm方言添加import语句即可。请参见下面的完整mssql连接。
import (
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mssql"
)
func main() {
db, err := gorm.Open("mssql", "sqlserver://username:password@localhost:1433?
database=dbname")
defer db.Close()
}
有关详细信息,请参阅gorm文档。 http://gorm.io/docs/connecting_to_the_database.html`