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

Wrong numeric data type mapping #131

Open
gsbingo17 opened this issue Dec 8, 2024 · 0 comments
Open

Wrong numeric data type mapping #131

gsbingo17 opened this issue Dec 8, 2024 · 0 comments
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@gsbingo17
Copy link

Verify the numeric data type mapping using the following schema and sample code.

package main

import (
	"fmt"

	"github.com/shopspring/decimal"

	spannergorm "github.com/googleapis/go-gorm-spanner"
	"gorm.io/gorm"

	"gorm.io/gorm/logger"
)

type Product struct {
	ID    string              `gorm:"column:id"`
	Name  string              `gorm:"column:name"`
	Price decimal.NullDecimal `gorm:"column:price;type:NUMERIC"`
}

func main() {

	// Replace with your actual Cloud Spanner project, instance, and database
	dsn := "projects/project-id/instances/test-202304/databases/testdb"

	db, err := gorm.Open(spannergorm.New(spannergorm.Config{
		DriverName: "spanner",
		DSN:        dsn,
	}), &gorm.Config{
		Logger: logger.Default.LogMode(logger.Info),
	})

	if err != nil {
		panic("failed to connect database")
	}

	// Insert a new product
	newProduct := Product{
		ID:    "2",
		Name:  "apple",
		Price: decimal.NullDecimal{Decimal: decimal.NewFromFloat(19.99), Valid: true},
	}

	if err := db.Create(&newProduct).Error; err != nil {
		panic(err)
	}

	fmt.Println("Inserted new product:", newProduct)

	// Query products where name is 'tom' with a limit
	var products []Product
	if err := db.Table("product").
		Clauses(spannergorm.ForceIndex("name_idx")).
		Where("name = ?", "apple").
		Limit(10).
		Find(&products).Error; err != nil {
		panic(err)
	}

	fmt.Println("Products with name 'apple' (limited to 10):", products)

}
create table products (
  id string(max),
  name string(max),
  price numeric
) primary key(id)

When the sample is executed, the following error is encountered.

spanner: code = "InvalidArgument", desc = "Value has type STRING which cannot be inserted into column price, which has type NUMERIC [at 1:62]\\nINSERT INTO `products` (`id`,`name`,`price`) VALUES (@p1,@p2,@p3)\\n
@gsbingo17 gsbingo17 added priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. labels Dec 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

No branches or pull requests

1 participant