Gin或Gorm抱怨唯一列索引是语法错误,有些不一致。

huangapple go评论74阅读模式
英文:

Gin or Gorm complain about a unique column index being a syntax error, somewhat inconsistently

问题

我从Gorm的文档中获取了一个创建唯一索引的示例,似乎只需在声明模型时在列标签中添加,unique即可。但是当我尝试运行它时,控制台总是输出以下消息:

(/Users/[...]/main.go:16) 
[2021-06-26 13:59:20]  near "unique": syntax error 

虽然直接从文档中获取的示例失败似乎很奇怪,但我尝试将该代码单独运行,它确实可以正常工作。然后,我逐渐添加了更多来自我的应用程序的代码,似乎在引入Gin-Gonic并调用gin.Default()后开始输出该消息。我不知道这是否仅因为Go默认不输出错误,还是存在某种冲突。但无论如何,我从未让Gorm实际创建唯一索引;无论是否有语法错误。

以下是最小可复现的代码,尽管它的行为相当不一致,大约有5次中有1次没有任何错误:

package main

import (
    "github.com/jinzhu/gorm"
    _ "github.com/jinzhu/gorm/dialects/sqlite"
    "github.com/gin-gonic/gin"
)

type User struct {
  gorm.Model
  Name string `gorm:"size:40;index:idx_name,unique"`
}

func main() {
    db, _ := gorm.Open("sqlite3", "test.db")
    db.AutoMigrate(&User{})

    r := gin.Default()
    r.Run(":8082")
}

如何解决这个问题?既要消除不一致的错误,又要实际创建唯一索引?

如果相关的话,我在Mac上运行这个代码。

英文:

I took the example from Gorm's docs of how to create a unique index, which seems to be be simply adding a ,unique to the column tag when declaring a model. But when I tried to run it, it would always output the following message in the console:

(/Users/[...]/main.go:16) 
[2021-06-26 13:59:20]  near "unique": syntax error 

While it seemed bizarre that an example directly from their docs would fail, I tried running that code in isolation, and it indeed worked fine on its own. Then, adding on more and more code from my app, it seemed to start outputting that message once Gin-Gonic was introduced and gin.Default() was called. I don't know if this is only because Go won't output the error by default, or there is some sort of a clash going on. But either way, I have also never had Gorm actually create the unique index; syntax error or not.

The minimum reproducible code is as follows, though it behaves rather inconsistently, running without any error about 1 out of 5 times:

package main

import (
    "github.com/jinzhu/gorm"
    _ "github.com/jinzhu/gorm/dialects/sqlite"
    "github.com/gin-gonic/gin"
)

type User struct {
  gorm.Model
  Name string `gorm:"size:40;index:idx_name,unique"`
}

func main() {
    db, _ := gorm.Open("sqlite3", "test.db")
    db.AutoMigrate(&User{})

    r := gin.Default()
    r.Run(":8082")
}

How would I go about fixing this; Both getting rid of the inconsistent error, and having the unique index actually being created?

If relevant, I'm running this on a Mac.

答案1

得分: 1

你从gorm.io中拿了一个例子,但是你没有使用正确的包导入。

在这里查看安装步骤:https://gorm.io/docs/#Install

你正在使用v1版本的导入(http://v1.gorm.io/docs/),并且使用最新版本的示例进行编码(http://gorm.io/docs/)。

请查看下面代码中的导入和数据库驱动初始化部分:

package main

import (
	"github.com/gin-gonic/gin"
	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
)

type User struct {
	gorm.Model
	Name string `gorm:"size:40;index:idx_name,unique"`
}

func main() {
	db, _ := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{})
	db.AutoMigrate(&User{})

	r := gin.Default()
	r.Run("localhost:8082")
}

英文:

You took an example from the gorm.io but you didn't use the right packages imports.

See here the installation here: https://gorm.io/docs/#Install

You are using imports from v1 (http://v1.gorm.io/docs/) and coding with examples from the latest version. (http://gorm.io/docs/)

Look the import and the database drive initialization in the code below:

package main

import (
	"github.com/gin-gonic/gin"
	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
)

type User struct {
	gorm.Model
	Name string `gorm:"size:40;index:idx_name,unique"`
}

func main() {
	db, _ := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{})
	db.AutoMigrate(&User{})

	r := gin.Default()
	r.Run("localhost:8082")
}

huangapple
  • 本文由 发表于 2021年6月26日 20:15:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/68142257.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定