大家的 SQL 数据类型碰到可为 NULL 都是如何设计的?

问题描述

当碰到可为 NULL 的字段时,源码 database/sql#NullInt64 只有 NullInt64 没有 NullUint64

当 user 表设计为:

CREATE TABLE `user` (
    `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
    `nickname` VARCHAR(50) NOT NULL,
    `phone` BIGINT(20) UNSIGNED NULL DEFAULT NULL,
);

我想 SQL 中的 UNSIGNED 应当对应 go 中的 uint64 才对,但看官方只给了 int 类型而没有提供 uint

大家有自定义 uint 类型解决表中的 UNSIGNED 无符号, 还是直接梭哈?

我当前解决办法

最后我图方便,引用 entgo 的 uint64

entgo 生成后的类型如下:

// User is the model entity for the User schema.
type User struct {
    config `json:"-"`
    // ID of the ent.
    ID uint64 `json:"id,omitempty"`
    // Phone holds the value of the "phone" field.
    Phone *uint64 `json:"phone,omitempty"`
    // Nickname holds the value of the "nickname" field.
    Nickname string `json:"nickname,omitempty"`
}
讨论数量: 5

仿照NullInt64自己定义个NullUint64不就行了,不想定义也可以用指针*uint64吧,简洁明了

1年前 评论
wonderfate (楼主) 1年前
DianWang

手机号码是定长字符串,为啥要用int类型

1年前 评论
renxiaotu 1年前
renxiaotu 1年前
wonderfate (楼主) 1年前
wonderfate (楼主) 1年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!