feat: adjust SetupJoinTable func if..else code (#4680)
This commit is contained in:
		
							parent
							
								
									5d91ddac8c
								
							
						
					
					
						commit
						c13f3011f9
					
				
							
								
								
									
										60
									
								
								gorm.go
									
									
									
									
									
								
							
							
						
						
									
										60
									
								
								gorm.go
									
									
									
									
									
								
							| @ -387,44 +387,46 @@ func (db *DB) SetupJoinTable(model interface{}, field string, joinTable interfac | |||||||
| 		modelSchema, joinSchema *schema.Schema | 		modelSchema, joinSchema *schema.Schema | ||||||
| 	) | 	) | ||||||
| 
 | 
 | ||||||
| 	if err := stmt.Parse(model); err == nil { | 	err := stmt.Parse(model) | ||||||
| 		modelSchema = stmt.Schema | 	if err != nil { | ||||||
| 	} else { |  | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
|  | 	modelSchema = stmt.Schema | ||||||
| 
 | 
 | ||||||
| 	if err := stmt.Parse(joinTable); err == nil { | 	err = stmt.Parse(joinTable) | ||||||
| 		joinSchema = stmt.Schema | 	if err != nil { | ||||||
| 	} else { |  | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
|  | 	joinSchema = stmt.Schema | ||||||
| 
 | 
 | ||||||
| 	if relation, ok := modelSchema.Relationships.Relations[field]; ok && relation.JoinTable != nil { | 	relation, ok := modelSchema.Relationships.Relations[field] | ||||||
| 		for _, ref := range relation.References { | 	isRelation := ok && relation.JoinTable != nil | ||||||
| 			if f := joinSchema.LookUpField(ref.ForeignKey.DBName); f != nil { | 	if !isRelation { | ||||||
| 				f.DataType = ref.ForeignKey.DataType |  | ||||||
| 				f.GORMDataType = ref.ForeignKey.GORMDataType |  | ||||||
| 				if f.Size == 0 { |  | ||||||
| 					f.Size = ref.ForeignKey.Size |  | ||||||
| 				} |  | ||||||
| 				ref.ForeignKey = f |  | ||||||
| 			} else { |  | ||||||
| 				return fmt.Errorf("missing field %s for join table", ref.ForeignKey.DBName) |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
| 
 |  | ||||||
| 		for name, rel := range relation.JoinTable.Relationships.Relations { |  | ||||||
| 			if _, ok := joinSchema.Relationships.Relations[name]; !ok { |  | ||||||
| 				rel.Schema = joinSchema |  | ||||||
| 				joinSchema.Relationships.Relations[name] = rel |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
| 
 |  | ||||||
| 		relation.JoinTable = joinSchema |  | ||||||
| 	} else { |  | ||||||
| 		return fmt.Errorf("failed to found relation: %s", field) | 		return fmt.Errorf("failed to found relation: %s", field) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	for _, ref := range relation.References { | ||||||
|  | 		f := joinSchema.LookUpField(ref.ForeignKey.DBName) | ||||||
|  | 		if f == nil { | ||||||
|  | 			return fmt.Errorf("missing field %s for join table", ref.ForeignKey.DBName) | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		f.DataType = ref.ForeignKey.DataType | ||||||
|  | 		f.GORMDataType = ref.ForeignKey.GORMDataType | ||||||
|  | 		if f.Size == 0 { | ||||||
|  | 			f.Size = ref.ForeignKey.Size | ||||||
|  | 		} | ||||||
|  | 		ref.ForeignKey = f | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	for name, rel := range relation.JoinTable.Relationships.Relations { | ||||||
|  | 		if _, ok := joinSchema.Relationships.Relations[name]; !ok { | ||||||
|  | 			rel.Schema = joinSchema | ||||||
|  | 			joinSchema.Relationships.Relations[name] = rel | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	relation.JoinTable = joinSchema | ||||||
|  | 
 | ||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 heige
						heige