Heim  >  Artikel  >  Backend-Entwicklung  >  Gorm: So speichern Sie Strukturen in Feldern

Gorm: So speichern Sie Strukturen in Feldern

WBOY
WBOYnach vorne
2024-02-06 08:15:11666Durchsuche

Gorm: So speichern Sie Strukturen in Feldern

Frageninhalt

Ich habe versucht, die Hederea-Vertrags-ID vom Typ *hedera.contractid im Gorm-Feld zu speichern, habe aber die Fehlermeldung „ Gefunden für Struktur github.com/hashgraph/hedera-sdk-go/v2 Ungültiges Feld. Feld erhalten Aliasschlüssel für Konto-ID: Definieren Sie einen gültigen Fremdschlüssel für die Beziehung oder implementieren Sie die Validierungsschnittstelle

Verpackungsvertrag

import (
    "fmt"

    "github.com/.../scanner/controllers/blockchain"
    database "github.com/.../scanner/db"
    model "github.com/.../scanner/models"
    "github.com/rs/xid"
    "gorm.io/gorm"
)

func deploycontract() *gorm.db {

    //connect to database
    db, err := database.connecttodb()

    //if db connection fails
    if err != nil {
        panic(err)
    }

    //init model
    var modelcontract model.contract

    //check if a contract has been deployed
    if err := db.first(&modelcontract); err.error != nil {
        //no deployment found

        //migrate the schema
        db.automigrate(&model.contract{})

        //deploy contract
        contract, _ := blockchain.deploycontract()

        //create record

        // generate random id
        id := xid.new()

        // create
        db.create(&model.contract{
            id:            id.string(),
            contractid:    contract.receipt.contractid,
            gasused:       contract.callresult.gasused,
            transactionid: fmt.sprint(contract.transactionid),
            timestamp:     contract.consensustimestamp,
            chargefee:     fmt.sprint(contract.transactionfee),
            payeraccount:  fmt.sprint(contract.transactionid.accountid),
            status:        fmt.sprint(contract.receipt.status),
        })

    }

    return db
}

gorm-Modell

package models

import (
    "time"

    "github.com/hashgraph/hedera-sdk-go/v2"
    "gorm.io/gorm"
)

type Contract struct {
    gorm.Model
    Id            string
    ContractId    *hedera.ContractID
    GasUsed       uint64
    TransactionId string
    Timestamp     time.Time
    ChargeFee     string
    PayerAccount  string
    Status        string
}

Richtige Antwort


Für einen benutzerdefinierten Datentyp müssen Sie angeben, wie der Wert in der Datenbank gespeichert und abgerufen werden soll. Dies geschieht durch die Implementierung der scannervaluer-Schnittstelle.

Allerdings dank hedera.contractid 是在另一个包中定义的,因此您需要创建自己的 contractid und der Implementierung dieser Schnittstellen. Dinge wie diese:

type contractid hedera.contractid

type contract struct {
    gorm.model
    id            string
    contractid    *contractid
    gasused       uint64
    transactionid string
    timestamp     time.time
    chargefee     string
    payeraccount  string
    status        string
}     

func (c *contractid) scan(value interface{}) error {
  bytes, ok := value.([]byte)
  if !ok {
    return errors.new(fmt.sprint("failed to unmarshal contractid value:", value))
  }

  return json.unmarshal(bytes, c)
}

func (c contractid) value() (driver.value, error) {
  return json.marshal(c)
}

Außerdem wird es hedera.contractid 转换为 model.contractid überall dort sein, wo es verwendet wird. Zum Beispiel:

cID := model.ContractID(*contract.Receipt.ContractID)

    // Create
    db.Create(&model.Contract{
        Id:            id.String(),
        ContractId:    &cID,
        GasUsed:       contract.CallResult.GasUsed,
        TransactionId: fmt.Sprint(contract.TransactionID),
        Timestamp:     contract.ConsensusTimestamp,
        ChargeFee:     fmt.Sprint(contract.TransactionFee),
        PayerAccount:  fmt.Sprint(contract.TransactionID.AccountID),
        Status:        fmt.Sprint(contract.Receipt.Status),
    })

Das obige ist der detaillierte Inhalt vonGorm: So speichern Sie Strukturen in Feldern. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Dieser Artikel ist reproduziert unter:stackoverflow.com. Bei Verstößen wenden Sie sich bitte an admin@php.cn löschen