首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >RTSP协议视频智能监控平台EasyNVR使用mysql数据源时gorm的自动迁移数据库表报错如何处理?

RTSP协议视频智能监控平台EasyNVR使用mysql数据源时gorm的自动迁移数据库表报错如何处理?

作者头像
EasyNVR
发布2021-01-21 10:37:00
发布2021-01-21 10:37:00
6290
举报
文章被收录于专栏:EasyNVREasyNVR

新版的EasyNVR默认都是使用的sqlite数据库,有的用户会问到我们,是否可以将sqlite数据库转化为mysql数据库使用,一般sqlite数据库已经足够大家的日常使用了,因此大家不要轻易更换数据库。

前端时间又用户反映使用mysql数据源时gorm的自动迁移数据库表报错,这是迁移数据库的一个弊端,下面我们看看如何解决。

数据库没有user,camera,hwnvr,roles,user_roles,role_camera,label,label_camera,advert这些表的时候,会自动创建这些数据表。

但是此处这些表都缺失,却只创建了一个数据库表,这明显有问题。

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 ’ip TEXT,port INTEGER,username TEXT,password TEXT,protocol TEXT DEFAULT’ at line 1

在启动时自动迁移数据库表时报错如上所示。我们搜索了这段报错,发现是sqlite和mysql数据库不通,在给数据库模型写法不兼容这两种数据库。因此我们修改一下写法,如下:

左侧修改前的,右侧修改后的。

代码语言:javascript
复制
type Camera struct {
   ID               uint   `gorm:"primary_key;type:INTEGER;not null"`
   Enable           uint   `gorm:"type:INTEGER"`
   Ondemand         uint   `gorm:"type:INTEGER"`
   Name             string `gorm:""`
   IP               string `gorm:""`
   Port             uint   `gorm:""`
   Username         string `gorm:"type:TEXT"`
   Password         string `gorm:"type:TEXT"`
   Protocol         string `gorm:"type:TEXT"`
   Rtsp             string `gorm:"type:TEXT"`
   RemoteRtmp       string `gorm:"type:TEXT"`
   RemoteRtsp       string `gorm:"type:TEXT"`
   Rtsprecord       string `gorm:"type:TEXT"` //for record rtsp url
   Onvif            string `gorm:"type:TEXT"`
   Cdn              string `gorm:"type:TEXT"`
   Reserve1         string `gorm:"type:TEXT"` // Audio
   Reserve2         string `gorm:"type:TEXT"`
   Reserve3         string `gorm:"type:TEXT"` // Record
   Reserve4         string `gorm:"type:TEXT"` //CdnEnable
   Reserve5         string `gorm:"type:TEXT"`
   Transport        string `gorm:"type:TEXT" binding:"eq=TCP|eq=UDP"`
   BackAudio        string `gorm:"type:TEXT"` //for backaudio name
   GbsEnable        uint   `gorm:"type:INTEGER"`
   GbsId            string `gorm:"type:TEXT"`
   RecordPlanEnable uint   `gorm:"type:INTEGER"`
   RecordPlan       string `gorm:"type:TEXT"`
}

这边将gorm中的default删除即可,有问题的几个字段的type字段也需要删除。

解决效果如下:

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-01-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档