feat: Ignore LastInsertID failed
LastInsertID not yet supported by some database drivers. Users could avoid errors through configuration. Close https://github.com/go-gorm/gorm/issues/6343
This commit is contained in:
		
							parent
							
								
									49b01a3e93
								
							
						
					
					
						commit
						273cf6491f
					
				| @ -1,6 +1,7 @@ | |||||||
| package callbacks | package callbacks | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
|  | 	"database/sql/driver" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"reflect" | 	"reflect" | ||||||
| 	"strings" | 	"strings" | ||||||
| @ -68,8 +69,8 @@ func Create(config *Config) func(db *gorm.DB) { | |||||||
| 			db.Statement.Build(db.Statement.BuildClauses...) | 			db.Statement.Build(db.Statement.BuildClauses...) | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		isDryRun := !db.DryRun && db.Error == nil | 		notDryRun := !db.DryRun && db.Error == nil | ||||||
| 		if !isDryRun { | 		if !notDryRun { | ||||||
| 			return | 			return | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| @ -126,6 +127,22 @@ func Create(config *Config) func(db *gorm.DB) { | |||||||
| 		insertOk := err == nil && insertID > 0 | 		insertOk := err == nil && insertID > 0 | ||||||
| 
 | 
 | ||||||
| 		if !insertOk { | 		if !insertOk { | ||||||
|  | 			if db.Config.IgnoreLastInsertIDWhenNotSupport { | ||||||
|  | 				_, rowsAffectedErr := driver.RowsAffected(0).LastInsertId() | ||||||
|  | 				if strings.Compare(err.Error(), rowsAffectedErr.Error()) == 0 { | ||||||
|  | 					return | ||||||
|  | 				} | ||||||
|  | 				_, resultNoRowsErr := driver.ResultNoRows.LastInsertId() | ||||||
|  | 				if strings.Compare(err.Error(), resultNoRowsErr.Error()) == 0 { | ||||||
|  | 					return | ||||||
|  | 				} | ||||||
|  | 				if db.Config.IsNotSupportLastInsertIDErr != nil && db.Config.IsNotSupportLastInsertIDErr(err) { | ||||||
|  | 					return | ||||||
|  | 				} | ||||||
|  | 				if db.Logger != nil { | ||||||
|  | 					db.Logger.Warn(db.Statement.Context, "Failed to get last insert ID, err: %v", err) | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
| 			if !supportReturning { | 			if !supportReturning { | ||||||
| 				db.AddError(err) | 				db.AddError(err) | ||||||
| 			} | 			} | ||||||
|  | |||||||
							
								
								
									
										6
									
								
								gorm.go
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								gorm.go
									
									
									
									
									
								
							| @ -24,6 +24,12 @@ type Config struct { | |||||||
| 	SkipDefaultTransaction    bool | 	SkipDefaultTransaction    bool | ||||||
| 	DefaultTransactionTimeout time.Duration | 	DefaultTransactionTimeout time.Duration | ||||||
| 
 | 
 | ||||||
|  | 	// Not all database support LastInsertId, you can set `IgnoreLastInsertIDWhenNotSupport` to true in those cases
 | ||||||
|  | 	IgnoreLastInsertIDWhenNotSupport bool | ||||||
|  | 	// When 'IgnoreLastInsertIDWhenNotSupport' is true, you can set `IsNotSupportLastInsertIDErr` to check if the error is 'NotSupportLastInsertID'
 | ||||||
|  | 	// Gorm only asserts the type of returned sql/driver.Result if 'IsNotSupportLastInsertIDErr' is not set.
 | ||||||
|  | 	IsNotSupportLastInsertIDErr func(error) bool | ||||||
|  | 
 | ||||||
| 	// NamingStrategy tables, columns naming strategy
 | 	// NamingStrategy tables, columns naming strategy
 | ||||||
| 	NamingStrategy schema.Namer | 	NamingStrategy schema.Namer | ||||||
| 	// FullSaveAssociations full save associations
 | 	// FullSaveAssociations full save associations
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Krisdiano
						Krisdiano