如何在gorm中制作一个可读但不可写的字段

问题描述 投票:0回答:1

DDL:

create table if not exists T
(
    A bigint,
    B bigint,
    C bigint generated always as (A + B) stored
);

型号:

type T struct {
    A int64
    B int64
    C int64
}

当我尝试插入此结构时,出现错误

ERROR: cannot insert into column "C" (SQLSTATE 42601)

如果我将C字段标记为

gorm:"-"
,那么我在读取时就得不到该字段的值。有没有一种方法不创建两个结构(一个用于写作,第二个用于阅读),而是使用一个结构?

postgresql go go-gorm
1个回答
0
投票

您可以使用字段级别权限

`gorm:"<-:false"`  // allow read, disable write permission
© www.soinside.com 2019 - 2024. All rights reserved.