大家的 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"`
}
仿照
NullInt64
自己定义个NullUint64
不就行了,不想定义也可以用指针*uint64
吧,简洁明了手机号码是定长字符串,为啥要用int类型