1

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.

3
  • 1
    have you tried defining the db tab? something like this: Owner string `db:"owner"` and then on the insert: ...VALUES (:owner, .... Commented Feb 5, 2020 at 3:01
  • Oh my god I'm dumb. Thanks, that was what I was missing. Commented Feb 5, 2020 at 3:06
  • glad to help, I will post it as the answer, could you please mark it Commented Feb 5, 2020 at 3:07

1 Answer 1

2

You could add the db tag to your struct:

 Owner string `db:"owner"`

and then on the insert:

...VALUES (:owner, ....
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.