duanlv5084 2018-01-16 11:27
浏览 89
已采纳

有什么方法可以创建单数数据库表?

  • development environment
    Lnaguage : Golang ver.1.9.2
    DB : mySQL
    Framework : not decided (Maybe I'll use revel)

  • situation
    I already have DB which has singular-name table ,like "user", "page". It can't be changed.

    Now I'll develop new application using this DB. I created simple application to connect this DB, and tried to auto migrate using gorm(https://github.com/jinzhu/gorm). I defined some models, like "user" which is same as existing DB table name, and run auto-migrate just as it written in (http://jinzhu.me/gorm/database.html#connecting-to-a-database )

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

    Then, new table "users" was created.

  • Question Can I create singular-name table, like "user" with auto-migrate or those things ? Using gorm is not required, so I'll use another orm library if it works.

I hope anyone help me !

  • 写回答

2条回答 默认 最新

  • duannei0044 2018-01-16 11:44
    关注

    Implement function TableName on struct User to return a custom name for the table. The ORM uses this function to get the table name for all DB operations.

    func (user *User) TableName() string {
        return "user"
    }
    

    Refer docs here: http://jinzhu.me/gorm/models.html#table-name-is-the-pluralized-version-of-struct-name

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码