Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditional Upsert not work #31

Open
cxcel opened this issue Aug 31, 2023 · 0 comments
Open

Conditional Upsert not work #31

cxcel opened this issue Aug 31, 2023 · 0 comments

Comments

@cxcel
Copy link

cxcel commented Aug 31, 2023

with git main branch(8ff4e72) 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)
    

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant