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 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。