Sterling Archer
Sterling Archer

Reputation: 22395

Cannot find Struct property that exists using Go/SQLX

I am gathering metadata from a file upload, and inserting into a DB table. The struct is as such:

// file Metadata struct
type MetaData struct {
    Owner       string
    FileRows    int64
    FileSize    string
    FileName    string
    FileUuid    string
    LastUpdated string
}

And here is how I am populating a reference to the struct:

metaDataRow := MetaData{
    Owner:       "Fake Name",
    FileRows:    (int64)(count - 1),
    FileSize:    fileSize,
    FileName:    fileName,
    FileUuid:    handle,
    LastUpdated: time.Now().Format(time.RFC822Z),
}

My issue is that when I try to insert this struct ref into my db, I get an error message:

could not find name Owner in &processor.MetaData{Owner:"Fake Name", FileRows:1499, FileSize:"308.9 kB", FileName:"small-file.csv", FileUuid:"1234567890qwerty", LastUpdated:"30 Jan 20 21:13 +0000"}

Now, as we can clearly see, Owner exists in the struct and it has a value, as do the others. I am not sure if the query assignment failed on the first lookup and panic'd there, but I can't get past this to see if the others fail as well. Here is my sqlx NamedExec as there may be an issue with my Struct reference, or the binding?

// execute transaction
_, err = tx.NamedExec(`
    INSERT INTO file_metadata (
        owner,
        file_rows,
        file_size,
        file_name,
        file_uuid,
        last_updated
    ) VALUES (
        :Owner,
        :FileRows,
        :FileSize,
        :FileName,
        :FileUuid,
        :LastUpdated
    )
`, &metaDataRow)

I am hoping this is an easy fix, borderline typo.

Upvotes: 1

Views: 1407

Answers (1)

andre
andre

Reputation: 447

You could add the db tag to your struct:

 Owner string `db:"owner"`

and then on the insert:

...VALUES (:owner, ....

Upvotes: 2

Related Questions