diff --git a/scope_private.go b/scope_private.go old mode 100644 new mode 100755 index 63fcea46..88632c3a --- a/scope_private.go +++ b/scope_private.go @@ -57,7 +57,12 @@ func (scope *Scope) buildWhereCondition(clause map[string]interface{}) (str stri str = strings.Replace(str, "?", strings.Join(tempMarks, ","), 1) default: if valuer, ok := interface{}(arg).(driver.Valuer); ok { - arg, _ = valuer.Value() + tp := reflect.TypeOf(arg) + if tp.Kind() != reflect.Struct && reflect.ValueOf(arg).IsNil() || arg == nil || valuer == nil { + arg = nil + } else { + arg, _ = valuer.Value() + } } str = strings.Replace(str, "?", scope.AddToVars(arg), 1) @@ -413,7 +418,7 @@ func (scope *Scope) related(value interface{}, foreignKeys ...string) *Scope { if relationship := fromField.Relationship; relationship != nil { if relationship.Kind == "many_to_many" { joinTableHandler := relationship.JoinTableHandler - scope.Err(joinTableHandler.JoinWith(toScope.db, scope.Value).Find(value).Error) + scope.Err(joinTableHandler.JoinWith(joinTableHandler, toScope.db, scope.Value).Find(value).Error) } else if relationship.Kind == "belongs_to" { sql := fmt.Sprintf("%v = ?", scope.Quote(toScope.PrimaryKey())) foreignKeyValue := fromFields[relationship.ForeignDBName].Field.Interface() @@ -530,7 +535,7 @@ func (scope *Scope) addForeignKey(field string, dest string, onDelete string, on var table = scope.TableName() var keyName = fmt.Sprintf("%s_%s_foreign", table, field) var query = `ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s ON DELETE %s ON UPDATE %s;` - scope.Raw(fmt.Sprintf(query, scope.QuotedTableName(), keyName, field, dest, onDelete, onUpdate)).Exec() + scope.Raw(fmt.Sprintf(query, scope.QuotedTableName(), scope.Quote(keyName), scope.Quote(field), scope.Quote(dest), onDelete, onUpdate)).Exec() } func (scope *Scope) removeIndex(indexName string) { @@ -548,7 +553,7 @@ func (scope *Scope) autoMigrate() *Scope { if !scope.Dialect().HasColumn(scope, tableName, field.DBName) { if field.IsNormal { sqlTag := scope.generateSqlTag(field) - scope.Raw(fmt.Sprintf("ALTER TABLE %v ADD %v %v;", quotedTableName, field.DBName, sqlTag)).Exec() + scope.Raw(fmt.Sprintf("ALTER TABLE %v ADD %v %v;", quotedTableName, scope.Quote(field.DBName), sqlTag)).Exec() } } scope.createJoinTable(field)