Search code examples
google-cloud-platformgoogle-cloud-spanner

Deleting row using composite key


I have the table 'column_defn' with the following schema. The keys are column_name,database_name and table_name

column_name             STRING(130)     NOT NULL
database_name           STRING(150)     NOT NULL
table_name              STRING(130)     NOT NULL
column_description      STRING(1000)    NOT NULL

I am trying to delete a row using the following REST request

{
  "session":"xxxxxxxxx"
  "singleUseTransaction": {
    "readWrite": {}
  },
  "mutations": [
    {
      "delete": {
        "table": "column_defn",
        "keySet": {
          "keys": [
            [
              {
                "column_name": "testd"
              },
              {
                "table_name": "test atbd"
              },
              {
                "database_name": "ASDFDFS"
              }
            ]
          ]
        }
      }
    }
  ]
}

but I keep getting the following error. Any idea as to where is wrong in the above request

{
  "error": {
    "code": 400,
    "message": "Invalid value for column database_name in table column_defn: Expected STRING.",
    "status": "FAILED_PRECONDITION"
  }
}

Update: The following request seems to be successful. At least it was returning the success code 200 and the commitTimestamp. However, the row didn't get deleted

{
  "singleUseTransaction": {
    "readWrite": {}
  },
  "mutations": [
    {
      "delete": {
        "table": "column_defn",
        "keySet": {
          "keys": [
            [
              "testd",
              "dsafd",
              "test atbd"
            ]
          ]
        }
      }
    }
  ]
}

Solution

  • keys should contain an array-of-arrays. In the outer array, there will be one entry for each row you are trying to delete. Each inner array will be the ordered list of key-values that define a single row (order matters). So in your example, you want:

    "keys": [["testd","ASDFDFS","test atbd"]]
    

    Note that the original question is inconsistent in the true ordering of the keys in the table. The above answer assumes the primary key is defined something like:

    PRIMARY KEY(column_name,database_name,table_name)