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

    新版的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数据库不通,在给数据库模型写法不兼容这两种数据库。因此我们修改一下写法,如下:

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

    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字段也需要删除。

    解决效果如下:

  • 相关阅读:
    map内置函数、lambda表达式、快捷生成想要的列表、filter内置函数
    python生成随机验证码
    Redis数据库之概念与创建服务
    JavaScript中的类
    python之with的使用
    PHP变量名区分大小写,函数名不区分大小写
    php curl基本操作
    PHP生成随机字符串包括大小写字母
    PHP多例模式
    一个关于动态编译时 Evidence的问题
  • 原文地址:https://www.cnblogs.com/EasyNVR/p/14282411.html
Copyright © 2011-2022 走看看