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

Migrator().ColumnTypes() not working inside Transaction #84

Closed
rny opened this issue Mar 8, 2022 · 1 comment
Closed

Migrator().ColumnTypes() not working inside Transaction #84

rny opened this issue Mar 8, 2022 · 1 comment
Assignees

Comments

@rny
Copy link

rny commented Mar 8, 2022

issue

I found AutoMigrate no longer works when put inside Transaction after recent updates of postgres driver. Then I found it is caused by Migrator().ColumnTypes() not work when it is put inside Transaction. example code below (sorry about the playground thing)

env

PostgreSQL 12.9 (Ubuntu 12.9-0ubuntu0.20.04.1)
gorm.io/driver/postgres v1.3.1
gorm.io/gorm v1.23.2

code

	type Email struct {
		gorm.Model
		Email string
	}

	result, ok := db.Migrator().ColumnTypes(&Email{})
	log.Printf("db:")
	log.Printf("ColumnTypes: %v", result)
	log.Printf("ColumnTypes error: %v", ok)

	// transaction
	if err := db.Transaction(func(tx *gorm.DB) error {
		result, ok := tx.Migrator().ColumnTypes(&Email{})
		log.Printf("Transaction:")
		log.Printf("ColumnTypes: %v", result)
		log.Printf("ColumnTypes error: %v", ok)
		return nil
	}); err != nil {
		return err
	}

output

2022/03/09 01:55:22 db:
2022/03/09 01:55:22 ColumnTypes: [{0xc0001fa230 {id true} {int8 true} { false} {true true} {false true} {true true} {64 true} {64 true} {0 true} {false true} <nil> { false} { false}} {0xc0001fa280 {created_at true} {timestamptz true} { false} {false true} {false true} {false false} {64 true} {6 true} {0 false} {true true} <nil> { false} { false}} {0xc0001fa2d0 {updated_at true} {timestamptz true} { false} {false true} {false true} {false false} {64 true} {6 true} {0 false} {true true} <nil> { false} { false}} {0xc0001fa320 {deleted_at true} {timestamptz true} { false} {false true} {false true} {false false} {64 true} {6 true} {0 false} {true true} <nil> { false} { false}} {0xc0001fa370 {email true} {text true} { false} {false true} {false true} {false false} {0 false} {0 false} {0 false} {true true} <nil> { false} { false}}]
2022/03/09 01:55:22 ColumnTypes error: <nil>

2022/03/09 01:55:22 /go/pkg/mod/gorm.io/driver/postgres@v1.3.1/migrator.go:331 driver: bad connection
[0.000ms] [rows:-] SELECT * FROM "emails" LIMIT 1
2022/03/09 01:55:22 Transaction:
2022/03/09 01:55:22 ColumnTypes: []
2022/03/09 01:55:22 ColumnTypes error: driver: bad connection
@jinzhu
Copy link
Member

jinzhu commented Oct 8, 2022

Should been fixed in latest version.

@jinzhu jinzhu closed this as completed Oct 8, 2022
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