Home > Enterprise >  How does spanner.Mutation understand which row to update
How does spanner.Mutation understand which row to update

Time:11-24

From the documentation:

Changing the values of columns in an existing row is very similar to inserting a new row:

m := spanner.Update("User",
  []string{"user_id", "profile"},
  []interface{}{UserID, profile})
_, err := client.Apply(ctx, []*spanner.Mutation{m})

How does the Spanner understand which row to update? I'm seeing like it is missing WHERE clause. Does it automatically use some fields as the key (like implicit user_id = "...")?

CodePudding user response:

Cloud Spanner will automatically use the primary key of the table that the mutation is updating. This means that you must include all the columns of the primary key in an Update mutation. One Update mutation will therefore also only update one row (and it will return a NOT_FOUND error if the row does not exist).

This also means that it is not possible to update the primary key value of a row. Instead, you must delete the row and insert a new one if you want to 'change' the primary key value.

See https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.v1#mutation for more information on how mutations work.

  • Related