spanner.Mutation如何理解要更新哪一行?

huangapple go评论91阅读模式
英文:

How does spanner.Mutation understand which row to update

问题

根据文档中的说明,Spanner使用主键来确定要更新的行。在示例代码中,UserID 是要更新的行的主键值,而 user_id 是列名。Spanner会根据提供的主键值来定位要更新的行,而不需要显式的 WHERE 子句。

英文:

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 = "...")?

答案1

得分: 1

Cloud Spanner将自动使用正在更新的变异表的主键。这意味着在Update变异中,您必须包含主键的所有列。因此,一个Update变异只会更新一行(如果该行不存在,则会返回NOT_FOUND错误)。

这也意味着无法更新行的主键值。相反,如果要“更改”主键值,您必须删除该行并插入一个新行。

有关变异如何工作的更多信息,请参见https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.v1#mutation。

英文:

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.

huangapple
  • 本文由 发表于 2022年11月23日 21:48:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/74547849.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定