尝试从模型创建表格时出现错误。

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

Error when trying to create tables from model

问题

我今天开始使用gorm玩耍,但不幸遇到了一些愚蠢的错误,卡在那里有一段时间了。
首先,我在Windows上运行MySQL 5(5.0.51b)和最新版本的go。我使用go get获取了gorm和mysql驱动程序,它们编译时没有错误,并且能够连接(可能是这样),但是每当我尝试基于声明的类型创建表时,它就会抛出一个错误,这个错误并不具有信息性(因为错误是由MySQL抛出的)。这是我的代码:

import (
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/jinzhu/gorm"
)

type User struct {
	id int
}

func main() {
	db, err := gorm.Open("mysql", "root:vertrigo@/shop?charset=utf8&parseTime=True&loc=Local")
	fmt.Println(err)

	a := db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&User{})

	fmt.Println(a)
}

所以这个模型的目标非常基本,例如一个只有一列的表。现在是输出:

&{0x111e2230 Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') ENGINE=InnoDB' at line 1 0 <nil> 0x112d4060 0x112d4000 0x112d8140 0 {0x112a3f20} <nil> false map[gorm:table_options:ENGINE=InnoDB] map[]}

第一个<nil>是连接错误,这意味着它能够连接,但是对于查询来说不太好,错误信息几乎没有提供任何有关gorm生成的SQL失败的原因。
所以问题是,如果有人知道为什么会出现错误,并且是否有任何解决方法。

英文:

I started to play with gorm today but to bad came across some silly error and am stuck on it for a while.
At first I am on windows and run MySQL 5 (5.0.51b) and latest version of go. I did go get for gorm and mysql driver and it compiles without an error and is able to connect (likely), but whenever I try to create a table based on declared type it throws an error which isn't informative (because the error is thrown by MySQL). Here is my code:

mport (
	&quot;fmt&quot;
	_ &quot;github.com/go-sql-driver/mysql&quot;
	&quot;github.com/jinzhu/gorm&quot;
)

type User struct {
	id int
}

func main() {
	db, err := gorm.Open(&quot;mysql&quot;, &quot;root:vertrigo@/shop?charset=utf8&amp;parseTime=True&amp;loc=Local&quot;)
	fmt.Println(err)

	a := db.Set(&quot;gorm:table_options&quot;, &quot;ENGINE=InnoDB&quot;).CreateTable(&amp;User{})

	fmt.Println(a)
}

So the model is aimed to be very basic, e.g. a table with one column. Now comes the output:

> < nil > &{0x111e2230 Error 1064: You have an error in your SQL syntax;
> check the manual that corresponds to your MySQL server version for the
> right syntax to use near ') ENGINE=InnoDB' at line 1 0 <nil>
> 0x112d4060 0x112d4000 0x112d8140 0 {0x112a3f20} <nil> false
> map[gorm:table_options:ENGINE=InnoDB] map[]}

The first <nil> is a connection error which means it's able to connect but with the query it's not that good and the error says almost nothing other than SQL generated by gorm fails for some reason.
So the question is if some one has an idea why the error is thrown and if there is any solution.

答案1

得分: 1

这个错误是在尝试创建一个空表时发生的,例如:

create table User() ENGINE=InnoDB

gorm 生成了一个空表的语句,因为 User 结构体的 id 字段是私有的。

id int 改为 ID int将结构体字段的首字母大写 可以将其暴露给外部包。


作为一个次要问题,不仅仅打印错误信息:

db, err := gorm.Open(...
fmt.Println(err)

最好检查每一个潜在的错误,并立即处理它们:

db, err := gorm.Open(...
if err != nil {
    log.Fatal(err)
}

对于其他错误,gorm 的错误处理 与大多数 Go 代码有些不同。你可能需要经常使用 db.GetErrors() 来检查错误。

英文:

That error occurs when trying to create an empty table, eg:

create table User() ENGINE=InnoDB

gorm generates an empty table statement because the id field of the User struct is private.

Change id int to ID int. Capitalising the first letter of a struct field exposes it to external packages.


As a secondary issue, instead of just printing the error:

db, err := gorm.Open(...
fmt.Println(err)

it's a good idea to check every potential error, and deal with them immediately:

db, err := gorm.Open(...
if err != nil {
    log.Fatal(err)
}

For other errors, gorm's error handling is a little unusual compared to most go code. You'll probably want to frequently check for errors using db.GetErrors().

huangapple
  • 本文由 发表于 2016年2月28日 04:13:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/35674903.html
匿名

发表评论

匿名网友

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

确定