Joel Parker
Joel Parker

Reputation: 297

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

I am trying to execute a select statement in Spanner and get this error:

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

I can't figure it out why it is complaining about the bind parameter because the code defines the bind parameter as a 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) + "}"

As you can see the value of requested_msg_id on line 1 is a string. Then on line 6 I define Msg_Id as a STRING bind parameter. Can anyone see what i’m missing ?

Upvotes: 0

Views: 574

Answers (1)

RedPandaCurios
RedPandaCurios

Reputation: 2324

In the SQL you are saying that the param 'record' is a struct with a Msg_id (@record.Msg_id) You are also saying that the type of param 'record' is a struct with a string 'Msg_id' value (line 5,6 and 15)

But then the value you pass for the param 'record' (lines 1 and 14) is a simple string, not a struct, hence the error.

You need to either say that the param is a simple string (say @Msg_id) in the SQL, and specify the type as a string in line 15... (Which would be simpler removing the need to define a record_type) or create the param value as a struct

Upvotes: 1

Related Questions