From 3ff1f31a77d906c1275c4fcc950c4a51d81ee43f Mon Sep 17 00:00:00 2001 From: Ant Kutschera <1108507+maxant@users.noreply.github.com> Date: Tue, 22 Oct 2024 23:58:32 +0200 Subject: [PATCH 1/2] rename column not working --- gen.go | 4 ++-- go.mod | 46 ++++++++++++++++++++---------------- my_test.go | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 22 deletions(-) create mode 100644 my_test.go diff --git a/gen.go b/gen.go index 3127d5d6..d5dccb39 100644 --- a/gen.go +++ b/gen.go @@ -2,7 +2,7 @@ package main import ( "gorm.io/gen" - "gorm.io/gen/examples/dal" +// "./gen/examples/dal" ) func generate() { @@ -12,7 +12,7 @@ func generate() { WithUnitTest: true, }) - g.UseDB(dal.DB) +// g.UseDB(dal.DB) g.ApplyBasic(Company{}, Language{}) // Associations g.ApplyBasic(g.GenerateModel("user"), g.GenerateModelAs("account", "AccountInfo")) diff --git a/go.mod b/go.mod index 159394d4..40e164ea 100644 --- a/go.mod +++ b/go.mod @@ -1,35 +1,41 @@ module gorm.io/playground -go 1.20 +go 1.22.0 + +toolchain go1.22.2 require ( - gorm.io/driver/mysql v1.5.2 - gorm.io/driver/postgres v1.5.2 - gorm.io/driver/sqlite v1.5.3 - gorm.io/driver/sqlserver v1.5.1 - gorm.io/gen v0.3.25 - gorm.io/gorm v1.25.4 + gorm.io/driver/mysql v1.5.7 + gorm.io/driver/postgres v1.5.9 + gorm.io/driver/sqlite v1.5.6 + gorm.io/driver/sqlserver v1.5.3 + gorm.io/gen v0.3.26 + gorm.io/gorm v1.25.12 ) require ( - github.com/go-sql-driver/mysql v1.7.1 // indirect + filippo.io/edwards25519 v1.1.0 // indirect + github.com/go-sql-driver/mysql v1.8.1 // indirect github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect github.com/golang-sql/sqlexp v0.1.0 // indirect + github.com/google/uuid v1.6.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect - github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect - github.com/jackc/pgx/v5 v5.4.3 // indirect + github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect + github.com/jackc/pgx/v5 v5.7.1 // indirect + github.com/jackc/puddle/v2 v2.2.2 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect - github.com/mattn/go-sqlite3 v1.14.17 // indirect - github.com/microsoft/go-mssqldb v1.5.0 // indirect - golang.org/x/crypto v0.14.0 // indirect - golang.org/x/mod v0.14.0 // indirect - golang.org/x/sys v0.14.0 // indirect - golang.org/x/text v0.13.0 // indirect - golang.org/x/tools v0.15.0 // indirect - gorm.io/datatypes v1.1.1-0.20230130040222-c43177d3cf8c // indirect - gorm.io/hints v1.1.0 // indirect - gorm.io/plugin/dbresolver v1.5.0 // indirect + github.com/mattn/go-sqlite3 v1.14.24 // indirect + github.com/microsoft/go-mssqldb v1.7.2 // indirect + golang.org/x/crypto v0.28.0 // indirect + golang.org/x/mod v0.21.0 // indirect + golang.org/x/sync v0.8.0 // indirect + golang.org/x/sys v0.26.0 // indirect + golang.org/x/text v0.19.0 // indirect + golang.org/x/tools v0.26.0 // indirect + gorm.io/datatypes v1.2.4 // indirect + gorm.io/hints v1.1.2 // indirect + gorm.io/plugin/dbresolver v1.5.3 // indirect ) replace gorm.io/gorm => ./gorm diff --git a/my_test.go b/my_test.go new file mode 100644 index 00000000..ca99daac --- /dev/null +++ b/my_test.go @@ -0,0 +1,68 @@ +package main + +import ( + "fmt" + "testing" + + gomysql "github.com/go-sql-driver/mysql" +) + +func getDatabaseSourceName() string { + username := "gorm" + password := "gorm" + host := "127.0.0.1" + port := "9910" + name := "gorm" + + cfg := gomysql.Config{ + User: username, + Passwd: password, + Net: "tcp", + Addr: host + ":" + port, + DBName: name, + Params: map[string]string{ + "charset": "utf8mb4", + "parseTime": "True", + "loc": "Local", + "multiStatements": "true", + }, + } + + url := cfg.FormatDSN() + + return url +} + +type Organisation2 struct { + ID string `gorm:"primaryKey"` + myName string `gorm:"column:NAME"` // <<<< THIS SHOULD WORK, BUT ISN'T +} + +func (Organisation2) TableName() string { + return "T_ORGANISATION" +} + +func TestNameLikeOtherTests(t *testing.T) { + + // create table and populate it + DB.Exec("CREATE TABLE IF NOT EXISTS T_ORGANISATION (ID VARCHAR(36) NOT NULL, NAME VARCHAR(100) NOT NULL, PRIMARY KEY (ID) ) ") + fmt.Println("created table") + DB.Exec("DELETE FROM T_ORGANISATION") + fmt.Println("deleted all organisations") + DB.Exec("INSERT INTO T_ORGANISATION (ID, NAME) VALUES ('A', 'NAME') ") + fmt.Println("inserted one organisation") + + var result Organisation2 + if err := DB.First(&result).Error; err != nil { + t.Errorf("Failed, got error: %v", err) + } + + if len(result.myName) <= 0 { + t.Errorf("Expected non-empty, got '%q'", result.myName) + } + + + +} + + From 59ed45169f4f3b02003df0893dd3cf91bfbc1bfc Mon Sep 17 00:00:00 2001 From: Ant Kutschera <1108507+maxant@users.noreply.github.com> Date: Wed, 23 Oct 2024 00:10:51 +0200 Subject: [PATCH 2/2] rename column not working --- my_test.go | 28 ---------------------------- 1 file changed, 28 deletions(-) diff --git a/my_test.go b/my_test.go index ca99daac..6f8969f4 100644 --- a/my_test.go +++ b/my_test.go @@ -3,36 +3,8 @@ package main import ( "fmt" "testing" - - gomysql "github.com/go-sql-driver/mysql" ) -func getDatabaseSourceName() string { - username := "gorm" - password := "gorm" - host := "127.0.0.1" - port := "9910" - name := "gorm" - - cfg := gomysql.Config{ - User: username, - Passwd: password, - Net: "tcp", - Addr: host + ":" + port, - DBName: name, - Params: map[string]string{ - "charset": "utf8mb4", - "parseTime": "True", - "loc": "Local", - "multiStatements": "true", - }, - } - - url := cfg.FormatDSN() - - return url -} - type Organisation2 struct { ID string `gorm:"primaryKey"` myName string `gorm:"column:NAME"` // <<<< THIS SHOULD WORK, BUT ISN'T