Model structs in models.go vs. MyFooRow #667
-
Maybe this is a very dumb question, but here's what I don't get: In Can anybody quickly shed some light on this? Am I doing something wrong? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 1 reply
-
My question is basically: |
Beta Was this translation helpful? Give feedback.
-
sqlc will generate per-query structs when they don't match any of the existing tables. Can you provide more information about your setup? What version of sqlc are you using? Can you post your schema, the problematic query, or the sqlc.json file? |
Beta Was this translation helpful? Give feedback.
-
I'm using version 1.5 with the dolphin parser. schema/persons.sql:
query/person.sql:
sqlc.yaml:
After
So, it is returning |
Beta Was this translation helpful? Give feedback.
-
Hmm, so when looking into this in more detail, I realised: My main confusion is still coming from the fact that in case of passing on the data (e.g. via a JSON REST API), I still need to work with all the |
Beta Was this translation helpful? Give feedback.
-
You're correct. The generated code will only return
sqlc provides a thin wrapper on top of your data model. That means that you'll need to handle NULL values and cases where you've written queries that return incomplete data. My suggestion is to have a separate set of structs that you return in your API instead of returning the database models directly. This layer of indirection allows you to change your database models without causing a change to your API. I also understand if you're looking for a higher-level database package. If that's the case, sqlc may not be a fit for you currently. |
Beta Was this translation helpful? Give feedback.
You're correct. The generated code will only return
Person
structs if you've selected all fields from the person table. If you select a sub-set of those fields, sqlc will create a query-specific struct to return.sqlc provides a thin wrapper on top of your data model. That means that you'll need to handle NULL values and cases where you've written queries that return incomplete data. My suggestion is to have a separate set of structs that you return in your API instead of returning the database models directly. This layer of indirection allows you to change your databa…