Skip to content

Conditional Upsert not work #31

@cxcel

Description

@cxcel

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

data := []map[string]interface{}{
{
"uid": "uid(v)",
"email": "user@company1.io",
"name": "first name"
},
}

userByEmailQuery := dqlx.Query(dqlx.EqFn("email", "user@company1.io")).
.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, "user@company1.io"))
}mu := &api.Mutation{ Cond: @if(eq(len(user), 1)), // Only mutate if "wrong_email@dgraph.io" 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)
    

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions