volatiletech / sqlboiler

Generate a Go ORM tailored to your database schema.
BSD 3-Clause "New" or "Revised" License
6.73k stars 544 forks source link

Passing an array of structs to a function #571

Closed tzachshabtay closed 5 years ago

tzachshabtay commented 5 years ago

What version of SQLBoiler are you using (sqlboiler --version)?

SQLBoiler v3.1.0

If this happened at generation time what was the full SQLBoiler command you used to generate your models? (if not applicable leave blank)

sqlboiler -c ./sqlboiler.toml -o ./internal/models --wipe psql

Description

I have a function in my postgres database which accepts an array of a custom type:


CREATE TYPE dbo.my_type AS (my_field bigint);

CREATE FUNCTION dbo.my_func(records dbo.my_type[])
...

Is there a way to call it with SQLBoiler? SQLBoiler did not generate a model for that type. I tried to do a raw query using types.Array and a custom written struct but got an error:

sql: converting argument $1 type: unsupported type dbo.MyType, a struct 

The code I tried:

type MyType struct {
   MyField int64 `boil:"my_field" json:"my_field"`
}

func bla() {
   ...
   records := []MyType{MyType{MyField: 1}}
   arr := types.Array(records)
   queries.Raw("SELECT dbo.my_func($1)", records).Exec(...)
}
aarondl commented 5 years ago

You need to do two things:

  1. Write a custom Go type to handle your postgresql type
  2. Write a replace rule in your config to have your custom postgresql type mapped to your new custom Go type

Should work if you do that.