gocassandrascyllagocqlgocqlx

Can’t update UDT using gocqlx


Hi so I have a usecase in which a user can create a secondary index on any table by calling an API. And I’m recording these indexes in a common table using map<text, frozen> where indexes is a User Defined Type:

CREATE TYPE indexes (
    local BOOLEAN,
    index_name TEXT,
    table_name TEXT,
    columns SET<TEXT>
);

When the API is called the secondary index is created. This part is working fine. But updating the row in the common tables is not. All the data in the UDT is stored as null:

 {'table_table1_20e24b85_d425_11e_int13_idx': {local: null, name: null, table_name: null, columns: null}}

Here’s my go code with gocqlx implementation (config.GetScylla() returns a gocqlx session):

selectedTable.Indexes[indexName] = models.IndexModel{
    Local:     reqBody.Local,
    Name:      indexName,
    TableName: selectedTable.InternalName,
    Columns:   reqBody.Columns,
}

stmt, names = qb.Update("tables").Set("indexes").
    Where(qb.Eq("internal_name"), qb.Eq("name"), qb.Eq("description")).ToCql()
if err := config.GetScylla().Query(stmt, names).BindStruct(&selectedTable).ExecRelease(); err != nil {
    utils.HandleErrorResponse(c, err)
    return
}

I hope the code is self-explanatory enough but all I’m doing is creating a map of string->indexes(UDT) to put in the common table and updating the common table with it.

Side note: why is the documentation on gocqlx so poor?


Solution

  • nevermind after being stuck on this for 2 days (I'm new to go and scylla both (by extension cassandra too), I found out that defining cql tags is necessary for UDTs. Isn't for tables but is for UDTs.

    type IndexModel struct {
        Local     bool     
        Name      string   
        TableName string   
        Columns   []string 
    }
    

    became

    type IndexModel struct {
        Local     bool     `cql:"local"`
        Name      string   `cql:"name"`
        TableName string   `cql:"table_name"`
        Columns   []string `cql:"columns"`
    }