Skip to content

Query, insert, and update varray failed or generated incorrect SQLs #87

@shelly-li-sl

Description

@shelly-li-sl

Query, insert, and update varray failed with the following errors:

#1, Reading an varray type using

      type EmailVarrayTable struct {
        	ID     uint     `gorm:"column:ID;primaryKey"`
        	Emails []string `gorm:"column:EMAILS;type:\"email_list_arr\""`
      }
      var got EmailVarrayTable
	if err := DB.First(&got, 1).Error; err != nil {
		t.Fatalf("Failed to select row: %v", err)
	}

Reported error:

2025/10/01 00:15:07 /scratch/shulili/GORM/gorm-github/gorm-oracle/tests/array_test.go:69 sql: Scan error on column index 1, name "EMAILS": unsupported Scan, storing driver.Value type *godror.Object into type *[]string
[5.321ms] [rows:1] SELECT * FROM "email_varray_tables" WHERE "email_varray_tables"."ID" = 1 ORDER BY "email_varray_tables"."ID" FETCH NEXT 1 ROW ONLY

#2, update an varray using:

newEmails := []string{"u1@ex.com", "u2@ex.com"}
	if err := DB.Model(&got).Update("Emails", newEmails).Error; err != nil {
		t.Fatalf("Failed to update emails: %v", err)
	}

The generated update sql is incorrect. This is the generated sql:
UPDATE "email_varray_tables" SET "EMAILS"=('u1@ex.com','u2@ex.com')
It should be:
UPDATE "email_varray_tables" SET "EMAILS"="email_list_arr"('u1@ex.com','u2@ex.com')

#3, insert an varray using:

item := EmailVarrayTable{
		ID:     1,
		Emails: []string{"alice_new@example.com", "bob_new@example.com", "gorm_new@oracle.com"},
	}
	if err := DB.Create(&item).Error; err != nil {
		t.Fatalf("Failed to insert row via GORM: %v", err)
	}

The generated insert sql is incorrect. This is the generated sql:
INSERT INTO "email_varray_tables" ("EMAILS","ID") VALUES (('alice_new@example.com','bob_new@example.com','gorm_new@oracle.com'),1) RETURNING "ID" INTO 0
It should be:
INSERT INTO "email_varray_tables" ("EMAILS","ID") VALUES ("email_list_arr"('alice_new@example.com','bob_new@example.com','gorm_new@oracle.com'),1) RETURNING "ID" INTO :id

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions