Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

carbon is used with grom to automatically create a table structure and report an error #164

Closed
uzdz opened this issue Jan 25, 2023 · 2 comments
Labels
Question Indicates that an issue, pull request, or discussion needs more information

Comments

@uzdz
Copy link

uzdz commented Jan 25, 2023

  • ORM框架:gorm
  • 数据库:Sqlite3
  • 平台:Windows10

在Mac上通过交叉编译生成Windows可执行文件。在Mac上运行程序代码是没报错且能生成表的,目前是在Windows上报错。

创建表:

return db.AutoMigrate(
		new(system.Menu),
	).Error

表Struct:

type Menu struct {
	MenuId   int    `json:"menuId" gorm:"column:menu_id;primary_key;auto_increment"` // 菜单编码
	Title    string `json:"title" gorm:"column:title;type:varchar(64);"`             // 菜单名称
	Path     string `json:"path" gorm:"column:path;type:varchar(128);"`              // 菜单路径
	Type     string `json:"type" gorm:"column:type;type:varchar(1);"`                // 菜单类型 F:页面 B:按钮
	ParentId int    `json:"parentId" gorm:"column:parent_id;type:int(11);"`          // 父权限编码
	Sort     int    `json:"sort" gorm:"column:sort;type:int(4);"`                    // 排序
	CreatedAt carbon.DateTime  `gorm:"column:create_time" json:"createTime"`
	UpdatedAt carbon.DateTime  `gorm:"column:update_time" json:"updateTime"`
}

报错内容:

panic: reflect.Value.Interface: cannot return value obtained from unexported field or method

goroutine 1 [running]:
reflect.valueInterface({0x168fe80?, 0xc000487240?, 0xc000008ca8?}, 0x6?)
/usr/local/go/src/reflect/value.go:1489 +0xd8
reflect.Value.Interface(...)
/usr/local/go/src/reflect/value.go:1478
github.com/jinzhu/gorm.(*sqlite3).DataTypeOf(0xc000008ca8, 0xc00001c990?)
/go/pkg/mod/github.com/jinzhu/gorm@v1.9.16/dialect_sqlite3.go:53 +0x256
github.com/jinzhu/gorm.(*Scope).createTable(0xc0000ef000)
/go/pkg/mod/github.com/jinzhu/gorm@v1.9.16/scope.go:1173 +0x169
github.com/jinzhu/gorm.(*Scope).autoMigrate(0xc0000ef000)
/go/pkg/mod/github.com/jinzhu/gorm@v1.9.16/scope.go:1269 +0xd6
github.com/jinzhu/gorm.(*DB).AutoMigrate(0xc00036fc70?, {0xc0004f5af8, 0xe, 0x0?})
/go/pkg/mod/github.com/jinzhu/gorm@v1.9.16/main.go:689 +0x8e
u1so-wy/pkg/common/database.AutoMigrate(0x0?)
/source/pkg/common/database/table.go:16 +0x2f6
u1so-wy/pkg/common/database.migrateModel({0xc00048bf46?, 0x16bfe17?})
/source/pkg/common/database/initialize.go:52 +0x85

感谢Carbon团队。

@uzdz uzdz added the Question Indicates that an issue, pull request, or discussion needs more information label Jan 25, 2023
@Issues-translate-bot Issues-translate-bot changed the title carbon搭配grom自动创建表结构报错 carbon is used with grom to automatically create a table structure and report an error Jan 25, 2023
@gouguoyin
Copy link
Member

Is it wrong not to use carbon?

@uzdz
Copy link
Author

uzdz commented Jan 29, 2023

问题已经解决,在carbon搭配gorm使用时,struct结构体中日期字段需要用gromtag标签type:datetime;描述。

type Menu struct {
	MenuId   int    `json:"menuId" gorm:"column:menu_id;primary_key;auto_increment"` // 菜单编码
	Title    string `json:"title" gorm:"column:title;type:varchar(64);"`             // 菜单名称
	Path     string `json:"path" gorm:"column:path;type:varchar(128);"`              // 菜单路径
	Type     string `json:"type" gorm:"column:type;type:varchar(1);"`                // 菜单类型 F:页面 B:按钮
	ParentId int    `json:"parentId" gorm:"column:parent_id;type:int(11);"`          // 父权限编码
	Sort     int    `json:"sort" gorm:"column:sort;type:int(4);"`                    // 排序
	CreatedAt carbon.DateTime  `gorm:"column:create_time;type:datetime;" json:"createTime"`
	UpdatedAt carbon.DateTime  `gorm:"column:update_time;type:datetime;" json:"updateTime"`
}

@uzdz uzdz closed this as completed Jan 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question Indicates that an issue, pull request, or discussion needs more information
Development

No branches or pull requests

2 participants