xorm映射struct 中的ID字段到数据库变成 i_d

tablecell · 2021-03-16 18:33:34 · 1367 次点击 · 大约8小时之前 开始浏览    置顶
这是一个创建于 2021-03-16 18:33:34 的主题,其中的信息可能已经有所发展或是发生改变。

package main

import (
    _ "github.com/mattn/go-sqlite3"

    "log"
    "xorm.io/xorm"
)

type User struct {
    ID        int64
    Name      string
    Age       int
    Author    string
    Published int
}
var x *xorm.Engine

func main() {

    x, err := xorm.NewEngine("sqlite3", "test.db")

    err = x.Sync2(new(User))
    if err != nil {
        log.Fatalf("Fail to sync database: %v\n", err)
    }

}

运行后查看数据库表 Screenshot_1.jpg


有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1367 次点击  
加入收藏 微博
9 回复  |  直到 2021-03-19 21:28:41
LintP
LintP · #1 · 4年之前

xorm 会将读取到的字段名转为下划线风格(蛇形)

tablecell
tablecell · #2 · 4年之前

1楼 @LintP 为什么 Name 不是 n_a_m_e

jarlyyn
jarlyyn · #3 · 4年之前
tablecelltablecell #2 回复

1楼 @LintP 为什么 Name 不是 n_a_m_e

试试 NAME

另外,用orm创建数据库表太不靠谱……

a7505553
a7505553 · #4 · 4年之前
1. type User struct {
    Id        int64 //可以定义成Id
    Name      string
    Age       int
    Author    string
    Published int
}

2 . type User struct {
    Id        int64  `xorm:"'id' "` //在xorm标签上面加一个'id'
    Name      string
    Age       int
    Author    string
    Published int
}

试一下

tablecell
tablecell · #5 · 4年之前
jarlyynjarlyyn #3 回复

#2楼 @tablecell 试试 NAME 另外,用orm创建数据库表太不靠谱……

创建数据库表是手工建吗?

LintP
LintP · #6 · 4年之前

@tablecell 建议看一下驼峰转蛇形的规则

alphayan
alphayan · #7 · 4年之前

就是大写字母转小写的规则 可以使用结构体tag指定

tablecell
tablecell · #8 · 4年之前
LintPLintP #6 回复

@tablecell 建议看一下驼峰转蛇形的规则

xorm怎么打印执行的Sql

LintP
LintP · #9 · 4年之前
tablecelltablecell #8 回复

#6楼 @LintP xorm怎么打印执行的Sql

添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传