第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將文本列的值指定為字符串并集?

如何將文本列的值指定為字符串并集?

Go
慕桂英546537 2022-09-26 17:21:12
我有一個結(jié)構(gòu):Documenttype Document struct {    ID               entity.EntityID    Type             string    ContentType      files.MIMEType    URL              string    UploadedByUserID entity.EntityID    CreatedAt        time.Time}type MIMEType stringconst (    MIMETypeJPG MIMEType = "image/jpeg"    MIMETypePDF MIMEType = "application/pdf"    MIMETypePNG MIMEType = "image/png"    MIMETypeTXT MIMEType = "text/plain")它映射到一個表,其中列的類型為:documentsContentTypevarchar(1024)create table documents(    id bigint not null default nextval('documents_id_seq') PRIMARY KEY,    url varchar(1024) not null,    type varchar(1024) not null,    content_type varchar(1024) not null,    uploaded_by_user_id bigint,    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP not null);Create(&d)其中 是一個實例給我一個錯誤:dDocument[2.530ms] [rows:1] INSERT INTO "documents" ("type","content_type","url","uploaded_by_user_id","created_at") VALUES ('other_freight','image/png','https://lateralline-documents-dev.s3.us-west-2.amazonaws.com/doc-other_freight-2021_08_25__11_15_13_379569000.png','253608954016301056','2021-08-25 11:15:13.82') RETURNING "id"interface conversion: interface {} is files.MIMEType, not string我想告訴gorm,當我讀取和寫入字段時,它不僅僅是一個任意字符串,它應該是一個值。有沒有辦法做到這一點?我在其他 ORM 中也看到了此功能。documents.content_typefiles.MIMEType
查看完整描述

1 回答

?
波斯汪

TA貢獻1811條經(jīng)驗 獲得超4個贊

您可能需要查看 中的自定義類型(或檢查答案)。go-gorm

簡而言之,您需要為結(jié)構(gòu)實現(xiàn) 和 接口。ScannerValuerMIMEType

func (m *MIMEType) Scan(value interface{}) error {

  val, ok := value.(string)

  if !ok {

    return errors.New(fmt.Sprint("Failed to unmarshal string value:", value))

  }

  

  *m = MIMEType(val)

  return nil

}


func (m MIMEType) Value() (driver.Value, error) {

  return string(m), nil

}


查看完整回答
反對 回復 2022-09-26
  • 1 回答
  • 0 關(guān)注
  • 85 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號