Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How I can store array of strings using gorm-spanner #130

Open
nahuelmonserrat opened this issue Dec 5, 2024 · 2 comments
Open

How I can store array of strings using gorm-spanner #130

nahuelmonserrat opened this issue Dec 5, 2024 · 2 comments
Labels
priority: p3 Desirable enhancement or fix. May not be included in next release. type: question Request for information or clarification. Not an issue.

Comments

@nahuelmonserrat
Copy link

Thanks for stopping by to ask us a question! Please make sure to include:

  • What you're trying to do
    I'm trying to store a set of values in a column of type array string in Spanner using GORM, but I'm encountering an error.
  • What code you've already tried
type NewEntity struct {
	Field1 int64            `gorm:"primaryKey;autoIncrement:false;column:picture_key"`
	Field2 []string        `gorm:"type:ARRAY<STRING(MAX)>;column:models"`
	Field3 time.Time   `gorm:"column:updated_at"`
}

dbEntity := model.NewEntity(p)
err := db.Create(dbEntity).Error
if err != nil {
  panic(err)
}
  • Any error messages you're getting
spanner: code = \"InvalidArgument\", desc = \"Value has type STRUCT<STRING, STRING, STRING, ...> which cannot be inserted into column Field2, which has type ARRAY<STRING> [at 1:168]\\n...Field3`) VALUES (@p1,(@p2,@p3,@p4,@p5,@p6,@p7,@..

PLEASE READ: If you have a support contract with Google, please create an issue in the support console instead of filing on GitHub. This will ensure a timely response.

@nahuelmonserrat nahuelmonserrat added priority: p3 Desirable enhancement or fix. May not be included in next release. type: question Request for information or clarification. Not an issue. labels Dec 5, 2024
@pdarulewski
Copy link

pdarulewski commented Dec 11, 2024

@nahuelmonserrat I think I have the same problem as yours but with floats.

spanner: code = "InvalidArgument", desc = "Value has type STRUCT<FLOAT64, FLOAT64> which cannot be inserted into column data, which has type ARRAY

What I've been doing to workaround this is to implement Valuer for my array types - it converts the []float64 to spanner.NullFloat64 type:

type Vector []float64

func (v Vector) Value() (driver.Value, error) {
	vector := make([]spanner.NullFloat64, 0, len(v))

	for _, f := range v {
		vector = append(vector, spanner.NullFloat64{Float64: f, Valid: true})
	}

	return vector, nil
}

type Entity struct {
	Data Vector `gorm:"not null;type:ARRAY<FLOAT64>""`
}

I think go-gorm-spanner should work with all array types, also with both []float64 and pq.Float64Array.

@pdarulewski
Copy link

Looks like it's not related to the gorm integration but the core library: googleapis/go-sql-spanner#331

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p3 Desirable enhancement or fix. May not be included in next release. type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

2 participants