Merge 386bc9925c070ea36dae3f63488abb0fe8da3309 into 87fc1b24737a885147240041293603eceb844356

This commit is contained in:
Andrey Nering 2018-02-04 18:12:08 +00:00 committed by GitHub
commit a0ec14b9a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 0 deletions

16
main.go
View File

@ -611,6 +611,22 @@ func (s *DB) AddForeignKey(field string, dest string, onDelete string, onUpdate
return scope.db return scope.db
} }
// AddCheckConstraint Add check to the given scope, e.g:
// db.Model(&User{}).AddCheckConstraint("users_login_count_chk", "login_count >= 0")
func (s *DB) AddCheckConstraint(constraintName, check string) *DB {
scope := s.clone().NewScope(s.Value)
scope.addCheckConstraint(constraintName, check)
return scope.db
}
// DropCheckConstraint Drop check constraint, e.g:
// db.Model(&User{}).DropCheckConstraint("users_login_count_chk")
func (s *DB) DropCheckConstraint(constraintName string) *DB {
scope := s.clone().NewScope(s.Value)
scope.dropCheckConstraint(constraintName)
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 // 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 { func (s *DB) Association(column string) *Association {
var err error var err error

View File

@ -1175,6 +1175,25 @@ 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() scope.Raw(fmt.Sprintf(query, scope.QuotedTableName(), scope.quoteIfPossible(keyName), scope.quoteIfPossible(field), dest, onDelete, onUpdate)).Exec()
} }
func (scope *Scope) addCheckConstraint(constraintName, check string) {
q := fmt.Sprintf(
"ALTER TABLE %s ADD CONSTRAINT %s CHECK (%s)",
scope.QuotedTableName(),
scope.quoteIfPossible(constraintName),
check,
)
scope.Raw(q).Exec()
}
func (scope *Scope) dropCheckConstraint(constraintName string) {
q := fmt.Sprintf(
"ALTER TABLE %s DROP CONSTRAINT %s",
scope.QuotedTableName(),
scope.quoteIfPossible(constraintName),
)
scope.Raw(q).Exec()
}
func (scope *Scope) removeIndex(indexName string) { func (scope *Scope) removeIndex(indexName string) {
scope.Dialect().RemoveIndex(scope.TableName(), indexName) scope.Dialect().RemoveIndex(scope.TableName(), indexName)
} }