Skip to content

a better way to pass transaction #1347

@cruvie

Description

@cruvie

I have to use .Table("") to remove table with tx.

func method1( ){
	err := query_moment.Q.Transaction(func(tx *query_moment.Query) error {
		err := tx.Moment.Create() // insert into `moment` xxxx
		err = method2(tx.Moment.UnderlyingDB())
		return err
	})
}
func method2( tx *gorm.DB)error{
	return query_mgr.Use(tx).Operation.Create() //❌ insert into `moment` xxxx
}


func method1( ){
	err := query_moment.Q.Transaction(func(tx *query_moment.Query) error {
		err := tx.Moment.Create() // insert into `moment` xxxx
		err = method2(tx.Moment.UnderlyingDB().Table(""))//⚠️
		return err
	})
}
func method2( tx *gorm.DB)error{
	return query_mgr.Use(tx).Operation.Create() //✅ insert into `operation` xxxx
}

provide a better way to pass Transaction

use .Table("") it's not a good way, looks ugly and easy to forget

#1338
#653
#566

Metadata

Metadata

Assignees

No one assigned

    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