Sqlx "missing destination name" for struct tag through pointer

Viewed 11490

I have a model like this:

type Course struct {
    Name string `db:"course_name"`
}

type Group struct {
    Course *Course
}
type Groups []Group

And when i try to do sqlx.Select for Groups with a query like this:

SELECT c.name as course_name FROM courses as c;

I get

missing destination name course_name in *main.Groups

error.

What is the issue with this code?

3 Answers

Ok, so when you are using an embbeded struct with Sqlx, capitalization is important, at least in my case.

You need to refer to the embedded struct in your sql, like so:

select name as "course.name" from courses...

Notice that course is lower case and the naming involves a dot/period between table and column.

Then your Get or Select should work just fine.

Related