扳手 Python 客户端 "Invalid Value for Bind Parameter Record Error"

Spanner Python Client "Invalid Value for Bind Parameter Record Error"

我试图在 Spanner 中执行 select 语句并收到此错误:

InvalidArgument: 400 Invalid value for bind parameter record: Expected STRUCT<Msg_id STRING>

我不明白为什么它抱怨绑定参数,因为代码将绑定参数定义为字符串。

    requested_msg_id = str(msg['Message Id'])
    rc = ResultCode.SUCCESS
    ## SELECT MessageSender, Message FROM MESSAGE_STORE where MessageId = msg_id

    record_type = param_types.Struct([
        param_types.StructField('Msg_id', param_types.STRING)
    ])

    with self.client.snapshot() as snapshot:
        try:
            results = snapshot.execute_sql(
                "SELECT MessageSender, Message from MESSAGE_STORE "
                "WHERE MessageId = @record.Msg_id LIMIT 1",
                params={'record' : (requested_msg_id)},
                param_types={'record' : record_type})
        except Exception as fetch_exception:
             rc = ResultCode.ERR_NO_MSG_FOUND

    # results is an interator
    for row in results:
        if row: 
            output = "{ 'Message Id':" + requested_msg_id + ", 'Sender':" + row[1] + ", 'Message':" + row[2] + ", 'Result Code':" + str(rc.value) + "}"

如你所见,第 1 行 requested_msg_id 的值是一个字符串。然后在第 6 行,我将 Msg_Id 定义为 STRING 绑定参数。谁能看到我错过了什么?

在 SQL 中你说参数 'record' 是一个带有 Msg_id (@record.Msg_id) 的结构 您还说参数 'record' 的类型是具有字符串 'Msg_id' 值的结构(第 5,6 和 15 行)

但是您为参数 'record' 传递的值(第 1 行和第 14 行)是一个简单的字符串,而不是结构,因此会出现错误。

您需要在 SQL 中说明参数是一个简单的字符串(例如 @Msg_id),并在第 15 行中将类型指定为字符串...(这将更简单,无需定义 record_type) 或者将参数值创建为结构