Merge d861d8737e0960731158733c0feee8aafd935b42 into b33f30714baeee37211f857f7135c78a394ed8d0
This commit is contained in:
		
						commit
						c38eeb9990
					
				@ -146,6 +146,13 @@ db.AutoMigrate(&User{}, &Product{}, &Order{})
 | 
				
			|||||||
// Add index
 | 
					// Add index
 | 
				
			||||||
db.Model(&User{}).AddIndex("idx_user_name", "name")
 | 
					db.Model(&User{}).AddIndex("idx_user_name", "name")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Add foreign key
 | 
				
			||||||
 | 
					// 1st param : foreignkey field
 | 
				
			||||||
 | 
					// 2nd param : destination table(id)
 | 
				
			||||||
 | 
					// 3rd param : ONDELETE
 | 
				
			||||||
 | 
					// 4th param : ONUPDATE
 | 
				
			||||||
 | 
					db.Model(&User{}).AddForeignKey("user_id", "destination_table(id)", "CASCADE", "RESTRICT")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Multiple column index
 | 
					// Multiple column index
 | 
				
			||||||
db.Model(&User{}).AddIndex("idx_user_name_age", "name", "age")
 | 
					db.Model(&User{}).AddIndex("idx_user_name_age", "name", "age")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										11
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								main.go
									
									
									
									
									
								
							@ -400,6 +400,17 @@ func (s *DB) AddIndex(indexName string, column ...string) *DB {
 | 
				
			|||||||
	return s
 | 
						return s
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					Add foreign key to the given scope
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Example:
 | 
				
			||||||
 | 
						db.Model(&User{}).AddForeignKey("city_id", "cities(id)", "RESTRICT", "RESTRICT")
 | 
				
			||||||
 | 
					*/
 | 
				
			||||||
 | 
					func (s *DB) AddForeignKey(field string, dest string, onDelete string, onUpdate string) *DB {
 | 
				
			||||||
 | 
						s.clone().NewScope(s.Value).addForeignKey(field, dest, onDelete, onUpdate)
 | 
				
			||||||
 | 
						return s
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (s *DB) AddUniqueIndex(indexName string, column ...string) *DB {
 | 
					func (s *DB) AddUniqueIndex(indexName string, column ...string) *DB {
 | 
				
			||||||
	s.clone().NewScope(s.Value).addIndex(true, indexName, column...)
 | 
						s.clone().NewScope(s.Value).addIndex(true, indexName, column...)
 | 
				
			||||||
	return s
 | 
						return s
 | 
				
			||||||
 | 
				
			|||||||
@ -630,6 +630,20 @@ func (scope *Scope) addIndex(unique bool, indexName string, column ...string) {
 | 
				
			|||||||
	scope.Raw(fmt.Sprintf("%s %v ON %v(%v);", sqlCreate, indexName, scope.QuotedTableName(), strings.Join(columns, ", "))).Exec()
 | 
						scope.Raw(fmt.Sprintf("%s %v ON %v(%v);", sqlCreate, indexName, scope.QuotedTableName(), strings.Join(columns, ", "))).Exec()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (scope *Scope) addForeignKey(field string, dest string, onDelete string, onUpdate string) {
 | 
				
			||||||
 | 
						var table string = scope.TableName()
 | 
				
			||||||
 | 
						var keyName string = fmt.Sprintf("%s_%s_foreign", table, field)
 | 
				
			||||||
 | 
						var query string = `
 | 
				
			||||||
 | 
							ALTER TABLE %s
 | 
				
			||||||
 | 
							ADD CONSTRAINT %s
 | 
				
			||||||
 | 
							FOREIGN KEY (%s)
 | 
				
			||||||
 | 
							REFERENCES %s
 | 
				
			||||||
 | 
							ON DELETE %s
 | 
				
			||||||
 | 
							ON UPDATE %s;
 | 
				
			||||||
 | 
						`
 | 
				
			||||||
 | 
						scope.Raw(fmt.Sprintf(query, table, keyName, field, dest, onDelete, onUpdate)).Exec()
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (scope *Scope) removeIndex(indexName string) {
 | 
					func (scope *Scope) removeIndex(indexName string) {
 | 
				
			||||||
	scope.Dialect().RemoveIndex(scope, indexName)
 | 
						scope.Dialect().RemoveIndex(scope, indexName)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user