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

    解决效果如下:

  • 相关阅读:
    常用算法解析-动态规划
    转载-通过ApplicationContext 去获取所有的Bean
    什么是crud?
    new 关键字 和 newInstance() 方法的 区别
    Java反射简单使用--第一次细致阅读底层代码
    动态创建管理定时任务-已完成
    springboot mail整合freemark实现动态生成模板
    20190930开始记录每天学习状态,更新至20200125结束
    hibernate的对象/关系映射结果为空,exists查不到值的问题-20190823
    转载-Java中LinkedList的一些方法—addFirst addFirst getFirst geLast removeFirst removeLast
  • 原文地址:https://www.cnblogs.com/EasyNVR/p/14282411.html
Copyright © 2011-2022 走看看