dqlx icon indicating copy to clipboard operation
dqlx copied to clipboard

Conditional Upsert not work

Open cxcel opened this issue 2 years ago • 0 comments

with git main branch(8ff4e7278708424691710a87468b2c44b4d0622f) code and dgraph verion 23.1.0. Conditional Upsert not work

data := []map[string]interface{}{ { "uid": "uid(v)", "email": "[email protected]", "name": "first name" }, }

userByEmailQuery := dqlx.Query(dqlx.EqFn("email", "[email protected]")). .Select(v as uid name)

condition := dqlx.Condition(dqlx.Eq("len(v)", "1"))

resp, err := db.Mutation(). Query(userByEmailQuery). Condition(condition). Set(data). Execute(ctx)

but i user dgo api, it works

query = `

query { user as var(func: eq(email, "[email protected]")) }mu := &api.Mutation{ Cond: @if(eq(len(user), 1)), // Only mutate if "[email protected]" belongs to single user. SetNquads: []byte(uid(user) "first name" .`), } req := &api.Request{ Query: query, Mutations: []*api.Mutation{mu}, CommitNow: true, }

// Update email only if exactly one matching uid is found.
_, err = dg.NewTxn().Do(ctx, req)

This solves it, I don't know if it will introduce other problems

diff --git a/predicate.go b/predicate.go index 96cd546..97c9031 100644 --- a/predicate.go +++ b/predicate.go @@ -221,22 +221,22 @@ func EscapePredicate(field string) string { predicate, alias, directive = parsePredicate(predicate)

            if alias != "" {
  •                   alias = fmt.Sprintf("<%s>:", alias)
    
  •                   alias = fmt.Sprintf("%s:", alias)
              }
    
  •           return fmt.Sprintf("%s %s %s<%s>%s", varName, asKeyword, alias, predicate, directive)
    
  •           return fmt.Sprintf("%s %s %s%s%s", varName, asKeyword, alias, predicate, directive)
      }
    
      field, alias, directive = parsePredicate(field)
    
      if alias != "" {
    
  •           alias = fmt.Sprintf("<%s>:", alias)
    
  •           alias = fmt.Sprintf("%s:", alias)
      }
    
      if strings.HasPrefix(field, "expand(") {
              return fmt.Sprintf("%s%s%s", alias, field, directive)
      }
    
  •   return fmt.Sprintf("%s<%s>%s", alias, field, directive)
    
  •   return fmt.Sprintf("%s%s%s", alias, field, directive)
    

}

cxcel avatar Aug 31 '23 02:08 cxcel