英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论