Search code examples
postgresqlgogo-gorm

How to make a field in gorm that is readable but not written


DDL:

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

Model:

type T struct {
    A int64
    B int64
    C int64
}

When I try to insert this structure, I get an error ERROR: cannot insert into column "C" (SQLSTATE 42601).

If I mark the C field as gorm:"-", then I don't get the value of this field when reading. Is there a way not to create two structures (one for writing, the second for reading), but to do with one?


Solution

  • You can use field level permissions:

    `gorm:"<-:false"`  // allow read, disable write permission