简体   繁体   English

捕获的异常:关键路径元素名称“ __xyz__”无效

[英]Caught exception: The key path element name “__xyz__” is invalid

Using the PHP-GDS library, I receive an exception when trying to 'upsert' an Entity when the key is in the format "__xyz__". 使用PHP-GDS库,当密钥的格式为“ __xyz__”时,尝试“向上​​插入”实体时会收到异常消息。

Is there a way to work around this, as the key is a username which I may have no control over, and __xyz__ is a perfectly valid username. 有没有办法解决此问题,因为密钥是我可能无法控制的用户名,而__xyz__是完全有效的用户名。 Here's the code and the exception message: 这是代码和异常消息:

$username  =  "__xyz__";

$obj_user = new Entity();
$obj_user->setKeyName($username);
try {
  $result = $obj_user_store->upsert($obj_user);
}catch (Exception $e) {
   echo 'Caught exception: ',  $e->getMessage(), "\n";
   return false;
}

Exception: 例外:

Caught exception: The key path element name "__xyz__" is reserved.

Unfortunately no, you have to encode the username is some way. 不幸的是,不行,您必须以某种方式对用户名进行编码。 Or, I would recommend to use auto-generated keys, and store the user-provided data in values, not keys. 或者,我建议使用自动生成的键,并将用户提供的数据存储在值中,而不是键中。

Documentation is pretty bad for this case, there's only one mention about that, in docs for superseded Python API for Datastore: https://cloud.google.com/appengine/docs/python/datastore/entities : "Key names cannot begin and end with two underscores __*__ ". 对于这种情况,文档非常糟糕,在被取代的适用于数据存储的Python API的文档中,只有一处提及: https ://cloud.google.com/appengine/docs/python/datastore/entities:“键名不能以以两个下划线__*__ “结尾。 Java documentation doesn't say no word about that. Java文档对此一言不发。 Nor modern Python API. 也没有现代的Python API。 And I couldn't find any PHP documentation for Datastore. 而且我找不到Datastore的任何PHP文档。

But it is the same for all APIs, because exception happens on db-side, so it doesn't matter which language you use (it's all protobufs down there). 但这对于所有API都是相同的,因为异常发生在db端,所以使用哪种语言都无关紧要(那里的所有协议都是原始的)。

I know only one usage of double underscore: when you build a query, and want to sort ascending/descending by keys. 我只知道双下划线的一种用法:构建查询时,想按键对升序/降序进行排序。 You can do that by sorting by non-existing reserved key named __key__ . 您可以通过对名为__key__不存在的保留键进行排序来实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM