Search code examples
gocassandragocql

Passing a map as a value to insert into Cassandra


I'm trying to insert a map value into my Cassandra database. I'm using Go to write my client. Currently its throwing the error "can not marshal string into map(varchar, varchar)". I understand what the error is, but I can't resolve it. Here is the code that I've written.

if err := session.Query("INSERT INTO emergency_records
        (mapColumn)
        VALUES (?)",
        "{'key' : 'value'}").Exec();
         err != nil {
            log.Fatal(err)
        }

What I don't get is that I've written one query as a whole unbroken string and it works fine without throwing this error. Yet breaking it down with the question mark it throws the error. I know this is something simple that I'm just overlooking and couldn't find in the documentation, but any help would be great thanks.


Solution

  • I haven't used Go casandra client before but I guess passing map as a map instead of string should work:

    mapValue := map[string]string{"key": "value"}
    if err := session.Query("INSERT INTO emergency_records (mapColumn) VALUES (?)", mapValue).Exec(); err != nil {
        log.Fatal(err)
    }