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

Breaking change since 1.4.6: nil-Slices with custom driver.Valuer / sql.Scanner #159

Open
maja42 opened this issue Feb 15, 2023 · 3 comments
Assignees

Comments

@maja42
Copy link

maja42 commented Feb 15, 2023

This is a follow-up to #152, because the proposed "solution" / workaround doesn't work. The ticket was already closed though.

In PostgreSQL, the column has type JSONB.

go (simplified):

type ArgumentList []interface{}

func (a ArgumentList) Value() (driver.Value, error) {
	js, _ := a.MarshalJSON() // Returns a json-array. Nil-slices return "[]".
	return datatypes.JSON(js).Value()
}

func (a *ArgumentList) Scan(value interface{}) error {
	var js datatypes.JSON
	js.Scan(value)
	return json.Unmarshal(js, a)
}

func (ArgumentList) GormDataType() string {
	return datatypes.JSON{}.GormDataType()
}

func (ArgumentList) GormDBDataType(db *gorm.DB, field *schema.Field) string {
	return datatypes.JSON{}.GormDBDataType(db, field)
}

I'm already using datatypes.JSON in my custom serializer / scanner, as proposed by the other ticket.
However, they are never called for nil-slices.

Before 1.4.6, my serializers converted nil-slices to empty json-arrays.
Now, they are ignored, nil gets converted into a PostgreSQL null, and my not-null-constraints are triggered.
So this is definitely a breaking change.

@maja42 maja42 changed the title Breaking change since 1.4.6: Serializing nil-Slices to JSONB Breaking change since 1.4.6: nil-Slices with custom driver.Valuer / sql.Scanner Feb 15, 2023
@maja42
Copy link
Author

maja42 commented Feb 15, 2023

Temporary workaround for go.mod:

exclude gorm.io/gorm v1.24.4
exclude gorm.io/gorm v1.24.5
exclude gorm.io/driver/postgres v1.4.6
exclude gorm.io/driver/postgres v1.4.7

Maybe this is (also) a bug in gorm.io/gorm?

@maja42
Copy link
Author

maja42 commented Jul 24, 2023

Updated exclude list:

exclude (
	gorm.io/driver/postgres v1.4.6
	gorm.io/driver/postgres v1.4.7
	gorm.io/driver/postgres v1.4.8
	gorm.io/driver/postgres v1.5.0
	gorm.io/driver/postgres v1.5.1
	gorm.io/driver/postgres v1.5.2
	gorm.io/driver/postgres v1.5.3

	gorm.io/gorm v1.24.4
	gorm.io/gorm v1.24.5
	gorm.io/gorm v1.24.6
	gorm.io/gorm v1.25.0
	gorm.io/gorm v1.25.1
	gorm.io/gorm v1.25.2
)

@maja42
Copy link
Author

maja42 commented Nov 20, 2023

The list is getting longer:

exclude (
	gorm.io/driver/postgres v1.4.6
	gorm.io/driver/postgres v1.4.7
	gorm.io/driver/postgres v1.4.8
	gorm.io/driver/postgres v1.5.0
	gorm.io/driver/postgres v1.5.1
	gorm.io/driver/postgres v1.5.2
	gorm.io/driver/postgres v1.5.3
	gorm.io/driver/postgres v1.5.4

	gorm.io/gorm v1.24.4
	gorm.io/gorm v1.24.5
	gorm.io/gorm v1.24.6
	gorm.io/gorm v1.25.0
	gorm.io/gorm v1.25.1
	gorm.io/gorm v1.25.2
	gorm.io/gorm v1.25.3
	gorm.io/gorm v1.25.4
	gorm.io/gorm v1.25.5
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants