Add support to remove foreign key constraints
This commit is contained in:
		
							parent
							
								
									0a51f6cdc5
								
							
						
					
					
						commit
						2a5838f889
					
				
							
								
								
									
										8
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										8
									
								
								main.go
									
									
									
									
									
								
							@ -611,6 +611,14 @@ func (s *DB) AddForeignKey(field string, dest string, onDelete string, onUpdate
 | 
			
		||||
	return scope.db
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// RemoveForeignKey Remove foreign key from the given scope, e.g:
 | 
			
		||||
//     db.Model(&User{}).RemoveForeignKey("city_id", "cities(id)")
 | 
			
		||||
func (s *DB) RemoveForeignKey(field string, dest string) *DB {
 | 
			
		||||
	scope := s.clone().NewScope(s.Value)
 | 
			
		||||
	scope.removeForeignKey(field, dest)
 | 
			
		||||
	return scope.db
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Association start `Association Mode` to handler relations things easir in that mode, refer: https://jinzhu.github.io/gorm/associations.html#association-mode
 | 
			
		||||
func (s *DB) Association(column string) *Association {
 | 
			
		||||
	var err error
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										10
									
								
								scope.go
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								scope.go
									
									
									
									
									
								
							@ -1174,6 +1174,16 @@ func (scope *Scope) addForeignKey(field string, dest string, onDelete string, on
 | 
			
		||||
	scope.Raw(fmt.Sprintf(query, scope.QuotedTableName(), scope.quoteIfPossible(keyName), scope.quoteIfPossible(field), dest, onDelete, onUpdate)).Exec()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (scope *Scope) removeForeignKey(field string, dest string) {
 | 
			
		||||
	keyName := scope.Dialect().BuildForeignKeyName(scope.TableName(), field, dest)
 | 
			
		||||
 | 
			
		||||
	if !scope.Dialect().HasForeignKey(scope.TableName(), keyName) {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	var query = `ALTER TABLE %s DROP CONSTRAINT %s;`
 | 
			
		||||
	scope.Raw(fmt.Sprintf(query, scope.QuotedTableName(), scope.quoteIfPossible(keyName))).Exec()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (scope *Scope) removeIndex(indexName string) {
 | 
			
		||||
	scope.Dialect().RemoveIndex(scope.TableName(), indexName)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user